in reply to How to return multiple array from subroutine
You can't. Subs return a single list. You could return references to those arrays, though.
sub breakEmailAddress { my ($address) = shift(@_); my @components = split('@', $address); my @counter = split('@', $address); return (\@components, \@counter); } my ($first, $second) = breakEmailAddress('john@some.domain.com'); print "FirstArr: @$first\n"; print "SecondArr: @$second\n";
Don't forget to use my on the arrays, or else you'll always return a reference to the same arrays. Using my forces the creation of new arrays every call.
I removed the & from your subroutine call. Why are you telling Perl to ignore prototypes?
Update: Alternatively, you can pass references to your arrays to the sub, and have it fill up the arrays instead of returning values:
sub breakEmailAddress { my ($components, $counter, $address) = shift(@_); @$components = split('@', $address); @$counter = split('@', $address); } # breakEmailAddress(my @first, my @second, 'john@some.domain.com'); XX +X my(@first,@second); breakEmailAddress(\@first, \@second, 'john@some.domain.com'); # Fixed + thanks to shmem print "FirstArr: @first\n"; print "SecondArr: @second\n";
Update: Applied shmem's fix.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to return multiple array from subroutine
by shmem (Chancellor) on Jul 04, 2006 at 17:13 UTC | |
|
Re^2: How to return multiple array from subroutine
by Anonymous Monk on Mar 27, 2013 at 13:02 UTC |