Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Edward Wijaya
08-05-04 08:56 PM


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=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.

Report this thread to moderator Post Follow-up to this message
Old Post
Kamal Gupta
08-05-04 08:56 PM


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

Report this thread to moderator Post Follow-up to this message
Old Post
WilliamGunther@aol.com
08-05-04 08:56 PM


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


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

Report this thread to moderator Post Follow-up to this message
Old Post
Edward Wijaya
08-06-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:33 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.