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

Re: Return multiple arrays

Thread Previous | Thread Next
From:
Brett W. McCoy
Date:
February 5, 2002 08:00
Subject:
Re: Return multiple arrays
Message ID:
Pine.LNX.4.43.0202051103580.30304-100000@chapelperilous.net
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
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
There is no distinctly native American criminal class except Congress.
		-- Mark Twain


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About