Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Returning two arrays..by count0 (Friar) |
on Jan 04, 2002 at 20:57 UTC ( [id://136303]=note: print w/replies, xml ) | Need Help?? |
While impossiblerobot and tstock provided excellent solutions, it may help to understand why these solutions are needed. When you return() an array, it is sent back (so to speak) as a list. The variable @array provides a nice little grouping for all its elements. If we had @array = ('a', 'b'); and return()'ed it, the portion catching this value will get ('a', 'b') and that variable container, @array, is no more. What does this lead to? Well, given the following: You might expect @return_val to contain ('a', 'b')... But because the return value is put into a list, ('a', 'b', 'c', 'd') is returned from foo(). There aren't any @a or @b anymore to "draw the line" and distinguish one array from the other... it's just one big list. So anyhow, by returning scalars (strings, hash refs, array refs, etc), you always ensure that the elements of each of the return values (or the elements pointed to by them) stay in their container variables. =)
In Section
Seekers of Perl Wisdom
|
|