Greetings,

You cannot pass two arrays as the previous array will slurp (consume) all the 
elements. You need to go for references.

Here is one way to do it

[code]
use strict;
use warnings;

my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
&study (\@pets , \@numbers);
 
sub study {
 
     my ($animals, $digits) = @_;
 
     print "Animals = @$animals\n";
     print "Digits = @$digits\n";
}
[/code]

[output]
Animals = dogs cats horses
Digits = 1 2 3 4 5 6 7 8 9 10
[/output]
 
best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
 From: eventual <eventualde...@yahoo.com>
To: "beginners@perl.org" <beginners@perl.org> 
Sent: Saturday, 7 September 2013 4:41 PM
Subject: How do I pass arrays into a subroutine
 


Hi,
 
In the example below, how do I pass @pets and @numbers into the subroutine so 
that
@animals = @pets  and 
@digits = @numbers.
Thanks
 
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
&study (@pets , @numbers);
 
sub study {
 
     my (@animals, @digits) = (@_[0] , @_[1]);
 
     print "Animals = @animals\n";
     print "Digits = @digits\n";
}

Reply via email to