Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How to return multiple array from subroutine

by qsl (Scribe)
on Jul 04, 2006 at 13:01 UTC ( [id://559158]=perlquestion: print w/replies, xml ) Need Help??

qsl has asked for the wisdom of the Perl Monks concerning the following question:

Helping Monks,

When i tried to return multiple array from subroutine,the two array contents are available in the first array itself.Canyou please let me know how the two arrays can be get separately.Find the below code,
sub breakEmailAddress { my ($address) = shift(@_); @components = split('@', $address); @counter = split('@',$address); return (@components,@counter); } (@first,@second) = &breakEmailAddress('john@some.domain.com'); print"FirstArr:@first\n"; print"SecondArr: @second\n";
It prints,
FirstArr:john some.domain.com john some.domain.com SecondArr:
Advice me how to get the array individually.
Thanks,
qsl.

Replies are listed 'Best First'.
Re: How to return multiple array from subroutine
by ikegami (Patriarch) on Jul 04, 2006 at 13:04 UTC

    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.

      ikegami,don't submit bugs! ;-)

      sub breakEmailAddress { my ($components, $counter, $address) = @_; @$components = split('@', $address); @$counter = split('@', $address); } my(@first,@second); breakEmailAddress(\@first, \@second, 'john@some.domain.com'); print "FirstArr: @first\n"; print "SecondArr: @second\n";

      cheers,
      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      "Don't forget to use my on the arrays." This helped to solve a different problem I was facing. Thanks a lot!!
Re: How to return multiple array from subroutine
by dorward (Curate) on Jul 04, 2006 at 13:04 UTC

    Return an arraylist of array references. See perlref

    -- update for pedants :)

      <pedant>
      You mean a _list_ of array references
      </pedant>

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: How to return multiple array from subroutine
by izut (Chaplain) on Jul 04, 2006 at 13:05 UTC
    You have to return array references instead
    use Data::Dumper; sub function { my @arr_a = (1, 2, 3); my @arr_b = (4, 5, 6); return (\@arr_a, \@arr_b); } my ($arr_a, $arr_b) = function(); print Dumper $arr_a, $arr_b; __END__ $VAR1 = [ 1, 2, 3 ]; $VAR2 = [ 4, 5, 6 ];

    Igor 'izut' Sutton
    your code, your rules.

Re: How to return multiple array from subroutine
by madtoperl (Hermit) on Jul 04, 2006 at 13:05 UTC
    Hi,

    If you need to return multiple array,you should return the array by reference and in the calling place you have to de-reference it and use.otherwise the boundary may be getting collapsed and the whole array contents will be available in the first array itself.The below code will work fine,
    sub breakEmailAddress { my ($address) = shift(@_); @components = split('@', $address); @counter = split('@',$address); return (\@components,\@counter); } ($first,$second) = &breakEmailAddress('john@some.domain.com'); print"FirstArr=>@$first\n"; print"SecondArr=>@$second\n";
    Thanks and Regards,
    madtoperl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://559158]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-26 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found