For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > How to pass two arrays as arg in a subroutine?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to pass two arrays as arg in a subroutine?
Edward Wijaya

2004-08-05, 3:56 pm

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
Kamal Gupta

2004-08-05, 3:56 pm


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=0D
>
> my (@array1, @array2) =3D @_; # 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)=0D

For example when you have a piece of code like=0D

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

@arr3 =3D (@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:
=3D=3D=3D=3D=3D=3D=3D=3D
#!/usr/local/bin

use strict;

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

&mysub(\@arr1, \@arr2);

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

Output:
=3D=3D=3D=3D=3D=3D=3D
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:ewijaya@i2r.a-star.edu.sg]=0D
Sent: Thursday, August 05, 2004 3:37 PM
To: beginners@perl.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) =3D @_; # 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

--=0D
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>






Confidentiality Notice=0D

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=
Mailadmin@wipro.com immediately
and destroy all copies of this message and any attachments.
WilliamGunther@aol.com

2004-08-05, 3:56 pm

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
needed. perdoc perlsub for more info.

--
-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::Deparse
-Data::Dumper
-Clone
-Perl::Tidy
-Beautifier
-DBD::SQLite
Edward Wijaya

2004-08-06, 3:55 am

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

Regards
Edward WIJAYA


> Hope this is useful for you.
>
> For more on references go through "perldoc perlref"
>
> With Best regards,
> R. Kamal Raj Guptha.
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com