CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How 2 pass 2 lists to a function ?

 



yaniv_av
Novice

Dec 14, 2002, 8:23 AM

Post #1 of 7 (520 views)
How 2 pass 2 lists to a function ? Can't Post

I have some function that need to do some manipulation of two lists.
How can I pass the 2 lists to that function ?
If I'll write sub(@a,@b) - How would I know inside the function to split them into the original lists ?


thodi
stranger

Dec 14, 2002, 8:56 AM

Post #2 of 7 (517 views)
Re: [yaniv_av] How 2 pass 2 lists to a function ? [In reply to] Can't Post

You could pass references to the two lists:

Code
foo(\@list1, \@list2); 

sub foo {
my $listref1 = shift;
my $listref2 = shift;
my @list1 = @$listref1;
...
}



yaniv_av
Novice

Dec 14, 2002, 1:55 PM

Post #3 of 7 (515 views)
Re: [thodi] How 2 pass 2 lists to a function ? [In reply to] Can't Post

And suppose the function also needs 2 pass back 2 lists.
Inside the function the lists are defined with "my", so I can't send back there's reference (There memory is free when exit from the function)
So, the same question, but now the problem is to pass 2 lists from within a function to the caller-function...


thodi
stranger

Dec 14, 2002, 2:36 PM

Post #4 of 7 (511 views)
Re: [yaniv_av] How 2 pass 2 lists to a function ? [In reply to] Can't Post


In Reply To
Inside the function the lists are defined with "my", so I can't send back there's reference (There memory is free when exit from the function)

Have you tried that?


davorg
Thaumaturge / Moderator

Dec 15, 2002, 2:20 AM

Post #5 of 7 (507 views)
Re: [yaniv_av] How 2 pass 2 lists to a function ? [In reply to] Can't Post

It is impossible to pass two lists to a subroutine. A subroutine recieves exactly one list of arguments.

You can, however, pass two _arrays_ to a subroutine. You do this by passing references to the arrays.

You can't have references to lists. Lists and arrays are different things.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Dec 15, 2002, 2:22 AM

Post #6 of 7 (505 views)
Re: [thodi] How 2 pass 2 lists to a function ? [In reply to] Can't Post


In Reply To
You could pass references to the two lists:

Code
foo(\@list1, \@list2); 

sub foo {
my $listref1 = shift;
my $listref2 = shift;
my @list1 = @$listref1;
...
}


Those aren't lists. Those are arrays. You can't take a reference to a list. References and lists are different things in Perl and it's very important to distinguish between them.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Dec 15, 2002, 2:32 AM

Post #7 of 7 (504 views)
Re: [yaniv_av] How 2 pass 2 lists to a function ? [In reply to] Can't Post

Two points:

1/ You can return two lists from a subroutine by putting the lists into arrays and returning references to the arrays.

2/ Perl is cleverer about memory management than many other languages. As long as there is a reference to a variable in existance then that variable remains in memory. It's only really variable _names_ that are scoped, the values persist as long as you need them.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives