in reply to Re: How to return a two dimensional array from a function in Perl?
in thread How to return a two dimensional array from a function in Perl?

Thanks a lot for your suggestions ikegami.
  • Comment on Re^2: How to return a two dimensional array from a function in Perl?

Replies are listed 'Best First'.
Re^3: How to return a two dimensional array from a function in Perl?
by RedRiverRun (Initiate) on Nov 04, 2018 at 23:02 UTC
    Hi,

    I am trying to do a simple return of a multidimensional array. I hoping to keep it simple, clean, so that I may use channel programming AI like ZPX Engine. $LON is a multidimensional array for this example. Don't care about the array reference at [0]. Want to return $LON and cleanly receive it into a target from the subroutine. [Left Hand] = [Right Hand] programming style. Basically an exact copy without re-processing it again. $a = $b; done.

    Super simple example below, did not even put in strict and warnings.

    Thank you in advanced.

    $ListPeople = (); $ListPeople = &ListOfNames; $counter = 0; while( $ListPeople[$counter][0] ne "" ){ print "$counter. What is my Name: $ListPeople[$counter][0] $ListPe +ople[$counter][1] $ListPeople[$counter][2]\n\n"; $counter++; }; sub ListOfNames{ $LON = (); $LON[0][0] = "Tim"; $LON[0][1] = "O"; $LON[0][2] = "Tay"; $LON[1][0] = "John"; $LON[1][1] = "Fitz"; $LON[1][2] = "Gerald"; $LON[2][0] = "Gerald"; $LON[2][1] = "Fitz"; $LON[2][2] = "John"; return( $LON ); }; exit(0);

    Output Should look like

    0. What is my Name: Tim O Tay

    1. What is my Name: John Fitz Gerald

    2. What is my Name: Gerald Fitz John

      > Super simple example below, did not even put in strict and warnings.

      But using strict and warnings would have told you what's wrong.

      These are two different variables:

      $LON = (); $LON[0][0] = "Tim";

      Two solutions, either

      my $LON ; # scalar is array ref $LON->[0][0] = "Tim"; ... return $LON;

      or

      my @LON; # array $LON[0][0] = "Tim"; ... return \@LON; # return ref

      See perldsc and perlref for more

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      A version showing some variations of technique (tested under Perl 5.8.9):

      c:\@Work\Perl\monks>perl -e "use warnings; use strict; ;; my @ListPeople = ListOfNames(); ;; PERSON: for my $counter (0 .. $#ListPeople) { my $ar_person = $ListPeople[$counter]; last PERSON if $ar_person->[0] eq ''; print qq{$counter. What is my Name: @$ar_person \n\n}; }; ;; sub ListOfNames { my @LON; ;; $LON[0][0] = 'Tim'; $LON[0][1] = 'O'; $LON[0][2] = 'Tay'; ;; $LON[1] = [ 'John', 'Fitz', 'Gerald' ]; ;; $LON[2] = [ qw(Gerald Fitz John) ]; ;; push @LON, [ '', 'not', 'here' ], [ 'Billy', 'Bob', 'Thornton' ]; ;; return @LON; } " 0. What is my Name: Tim O Tay 1. What is my Name: John Fitz Gerald 2. What is my Name: Gerald Fitz John
      The  ListOfNames() function could simply be written as:
      sub ListOfNames { return [ 'Tim', 'O', 'Tay' ], [ 'John', 'Fitz', 'Gerald' ], [ qw(Gerald Fitz John) ], [ '', 'not', 'present' ], [ 'Billy', 'Bob', 'Thornton' ], ; }


      Give a man a fish:  <%-{-{-{-<