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
>
>     my (@array1, @array2) = @_;   # is this correct? How do I do it?
>

 @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([EMAIL PROTECTED], [EMAIL PROTECTED]);

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:[EMAIL PROTECTED]
Sent: Thursday, August 05, 2004 3:37 PM
To: [EMAIL PROTECTED]
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;
}

Please advice.

Thanks so much for your time.

Regards,
Edward WIJAYA
SINGAPORE

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>






Confidentiality Notice

The information contained in this electronic message and any attachments to this 
message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged 
information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to