# How to pass two arrays as arg in a subroutine? - PERL Beginners

Hi, Whenever you pass two arrays (or any number of arrays for that matter) as arguments to a function, what happens is they get flattened and become a single list and then it is assigned to _. So when U say   array1 has the entire _ (which is now having a single list that was constructed with the 2 arrays passed to the function) For example when you have a piece of code like arr1 = ("Sun", "Mon"); arr2 = ("Tue", "Wed"); arr3 = (arr1, arr2); Now arr3 has a single list with Sun, Mon, Tue and Wed as ...

1. ## RE: How to pass two arrays as arg in a subroutine?

Hi,

Whenever you pass two arrays (or any number of arrays for that matter)
as arguments to a function, what happens is they get flattened and
become a single list and then it is assigned to _.

So when U say

array1 has the entire _ (which is now having a single list that was
constructed with the 2 arrays passed to the function)

For example when you have a piece of code like

arr1 = ("Sun", "Mon");
arr2 = ("Tue", "Wed");

arr3 = (arr1, arr2);

Now arr3 has a single list with Sun, Mon, Tue and Wed as values. This
is because of the nature of Perl flattening arrays inside a list.

A solution for you is to pass the arrays as reference and then
dereference it inside the function...

Example:
========
#!/usr/local/bin

use strict;

my arr1 = (1, 2, 3, 4);
my arr2 = ('One', 'Two', 'Three', 'Four');

&mysub(\arr1, \arr2);

sub mysub {
my (\$a_1, \$a_2) = _;
for my \$var ({\$a_1}) {
print "\$var in words is \${\$a_2}[\$var-1]\n";
}
}

Output:
=======
1 in words is One
2 in words is Two
3 in words is Three
4 in words is Four

Hope this is useful for you.

For more on references go through "perldoc perlref"

With Best regards,
R. Kamal Raj Guptha.

-----Original Message-----
From: Edward Wijaya [mailto:a-star.edu.sg]
Sent: Thursday, August 05, 2004 3:37 PM
To: org
Subject: How to pass two arrays as arg in a subroutine?

Hi,

I have a subroutine that take 2 arrays as argument.
I dont' know how to construct it?

sub mysub{

my (array1, array2) = _; # is this correct? How do I do it?

#process array1
#process array2 etc

return array3;
}

Thanks so much for your time.

Regards,
Edward WIJAYA
SINGAPORE

--
To unsubscribe, e-mail: org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Confidentiality Notice

The information contained in this electronic message and any attachments tothis message are intended
for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
you are not the intended recipient, please notify the sender at Wipro com immediately
and destroy all copies of this message and any attachments.
Kamal Guest

2. ## Re: How to pass two arrays as arg in a subroutine?

Have them pass the arrays as a reference. For example:

array1 = (1, 2, 3, 4, 5);
array2 = (6, 7, 8, 9, 10);
mysub(\array1,\array2);

sub mysub{

my (\$array1, \$array2) = _;
#process {\$array1}
#process {\$array2} etc
return array3;
}

Look into perldoc perlref

Prototyping is another option, not entirely recommended unless it's entirely

--
-will
http://www.wgunther.tk
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Dep
-Data::Dumper
-Clone
-Perl::Tidy
-Beautifier
-DBD::SQLite
WilliamGunther@aol.com Guest

3. ## Re: How to pass two arrays as arg in a subroutine?

Thanks so much Kamal,
your explanation is very clear and complete.
It works now.
I learnt a great deal from it.

Regards
Edward WIJAYA

Edward Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•