David Buddrige wrote:
>
> Hi all,
Hello,
> I want to write a subroutine that takes an array and a hash as input
> parameters and then does some work on them. I can't seem to determine
> how to do this however; it seems that with a subroutine, all you can
> pass is a single array ( which appears as @_ ) to the subroutine.
You will have to pass references to arrays and hashes.
perldoc perlref
perldoc perlsub
> The program I am trying to write is below:
>
> #!/usr/bin/perl
use warnings;
use strict;
> sub mysub
> {
> @my_array = @_;
> %my_hash = %_;
my ( $array_ref, $hash_ref ) = @_;
> print "Print the array\n";
>
> foreach (@my_array)
foreach ( @$array_ref )
> {
> print "$_ \n";
> }
> print "Now print the hash\n";
>
> print $my_hash{"first"};
> print $my_hash{"second"};
> print $my_hash{"third"};
print $hash_ref->{'first'}, $hash_ref->{'second'},
$hash_ref->{'third'};
> }
>
> my %the_hash;
> my @the_array;
>
> $the_array[0] = 5;
> $the_array[1] = 43;
> $the_array[2] = 78;
>
> $the_hash{"first"} = "number1\n";
> $the_hash{"second"} = "foo\n";
> $the_hash{"third"} = "abc\n";
You can declare and define variables in one step:
my %the_hash = ( first => "number1\n", second => "foo\n", third =>
"abc\n" );
my @the_array = ( 5, 43, 78 );
> mysub (@the_array,%the_hash);
Now pass references to the sub:
mysub( \@the_array, \%the_hash );
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]