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.
In reply to Re: How to return multiple array from subroutine
by ikegami
in thread How to return multiple array from subroutine
by qsl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |