Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How to return multiple array from subroutine

by qsl (Scribe)
on Jul 04, 2006 at 13:01 UTC ( #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 (Pope) 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 (Pilgrim) 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
Node Status?
node history
Node Type: perlquestion [id://559158]
Approved by Corion
help
Chatterbox?
[LanX]: they?
[LanX]: click!
[choroba]: they
[LanX]: why do you think it's more than one person?
[Your Mother]: Because we are LEGION! Fix YOUR SIG SIG BLOCK!!!!
[Your Mother]: Forgot to sign in as my sockpuppet, how embarrassing!
[1nickt]: pryrt I am creating a Type to check valid user IDs, which must be a whole number greater than or equal to zero. I would like to disallow 1.0 but because of this behaviour, by the time it is checked by the constraint, it *is* an Int.
[LanX]: should this be considered? Re: Parsing .txt into arrays
[LanX]: and this Re^4: Hash user input
[pryrt]: LanX, I would vote "keep" if it were considered: it's not helpful, but it could be taken multiple ways, not all of which are offenseive...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-05-24 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?