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

How to return the two set of arrays to the main program at the same time ??

if there is a main program as follows :

 calculate(10,20,30);<br>
the subroutine is :

a[0] = $_[0]+$_[1];<br> a[1] = $_[0]-$_[1];<br> a[2] = $_[0]*$_[2];<br> b[0] = $_[1]+$_[2];<br> b[0] = $_[1]-$_[2];<br> <br>

i need to return these as @a, @b into the main progam at the same instance
from the above subroutine individually and recapture it in the main program ...!!

How can I achieve this ??
-sugar

Replies are listed 'Best First'.
Re: How to return the two set of arrays to the main program at the same time ??
by davorg (Chancellor) on Dec 07, 2005 at 16:08 UTC

    Return references to the arrays. See "perldoc perlreftut", "perldoc perlref" and "perldoc perlsub".

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

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

Re: How to return the two set of arrays to the main program at the same time ??
by holli (Abbot) on Dec 07, 2005 at 16:09 UTC
    return them as references:
    sub calculate { #do stuff return \@a, \@b; } my ($a, $b) = calculate(10,20,30); for ( @{$a} ) { print "a: $_\n"; } my @b = @{$b}; print "@b";


    holli, /regexed monk/
Re: How to return the two set of arrays to the main program at the same time ??
by philcrow (Priest) on Dec 07, 2005 at 16:08 UTC
    Try:
    sub { my ( @a, @b ); #... fill in the arrays return \@a, \@b; }

    Phil

Re: How to return the two set of arrays to the main program at the same time ??
by CountOrlok (Friar) on Dec 07, 2005 at 16:08 UTC
    Return as array refs
Re: How to return the two set of arrays to the main program at the same time ??
by ikegami (Patriarch) on Dec 07, 2005 at 16:43 UTC

    There's another way noone mentioned:

    calculate(\@array1, \@array2, 10, 20, 30); sub calculate { my ($array1, $array2, $i, $j, $k) = @_; @$array1 = (); @$array2 = (); $array1->[0] = $i + $j; $array1->[1] = $i - $j; $array1->[2] = $i * $k; $array2->[0] = $j + $k; $array2->[1] = $j - $k; }

    Note: $a and $b shouldn't be used as variable names. They conflict with special variables.