in reply to Returning two arrays..

One way of returning two (or more) arrays is by trying the following code. Note that in this example I am returning three array references.

#!/usr/local/bin/perl -w

use strict;

my ($f1,$f2,$f3)=foo();

print "arry1 arry2 arry3\n";
print "----- ----- -----\n";
for my $i (0..(scalar(@$f1)-1))
{
  print " @$f1[$i]    @$f2[$i]    @$f3[$i]\n";
}

sub foo
{
  my @$arry1=("a1","a2","a3");
  my @$arry2=("b1","b2","b3");
  my @$arry3=("c1","c2","c3");

  return @_=($arry1,$arry2,$arry3);
}

Should print out the following:

arry1 arry2 arry3
----- ----- -----
 a1    b1    c1
 a2    b2    c2
 a3    b3    c3
metadoktor

"The doktor is in."

Replies are listed 'Best First'.
Re: Re: Returning two arrays..
by davorg (Chancellor) on Jan 04, 2002 at 17:18 UTC
    return @_=($arry1,$arry2,$arry3);

    I wonder what the point of that assignment is? What advantages does it have over a simple:

    return $arry1, $arry2, $arry3;
    --
    <http://www.dave.org.uk>

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