develooper Front page | perl.beginners | Postings from February 2002

Re: Return multiple arrays

Thread Previous | Thread Next
Brett W. McCoy
February 5, 2002 08:00
Re: Return multiple arrays
Message ID:
On Tue, 5 Feb 2002, Mike Garner wrote:

> I'm going back through some old code and trying to clean things up, apply
> new tricks, and make sure everything is running in strict mode.  Doing this
> I have encountered a problem.  I have a subroutine that calls another
> subroutine, passing one array to the second for processing.  I need the
> second sub to return 3 arrays to the first sub.  I can't seem to get this
> to work....well I can return the data from all three subs, but it returns
> into one array.  Is there an option to do this or do I need to rewrite the
> second subroutine into 3 smaller subs? or maybe I'm not using my
> correctly...running without use strict; and not using my on the arrays
> makes them global and avaiable for processing by the first sub...but I want
> to use strict;

Returning three arrays directly will flatten out into one array (the same
goes for when you pass arrays into a function).  The way to get around
this is to pass references back and forth:

my $arrayref1 = [1, 2, 3];

my ($arrayref2, $arrayref3) = sub2($arrayref1);

sub sub2 {

	my($arrayref5, ...);
	my $arrayref1 = shift;

	return $arrayref6, $arrayref7;

-- Brett
There is no distinctly native American criminal class except Congress.
		-- Mark Twain

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About