http://www.perlmonks.org?node_id=1053766

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

I'm not sure what I'm doing wrong here. I'm running a while loop, and all I want to do is to add another array to a multidimensional array on each pass...

Here's the code:

while ($row = $sth->fetchrow_arrayref()) { @player = [(@$row[0], @$row[4])]; }

It only prints out the last record that the array ran through?

I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Loop that creates multidimensional array?
by keszler (Priest) on Sep 12, 2013 at 16:59 UTC
Re: Loop that creates multidimensional array?
by Laurent_R (Canon) on Sep 12, 2013 at 17:05 UTC

    Try this:

    push @player, [(@$row[0], @$row[4])];
      got it to work with this:
      while ($row = $sth->fetchrow_arrayref()) { @array = (@$row[0], @$row[4]); push @{ $players[$count] }, @array; }

        No need to copy the 2 items to a new array.

        while (my $row = $sth->fetchrow_arrayref) { push @{ $players[$count] }, $row->[0], $row->[4]; }
Re: Loop that creates multidimensional array?
by Anonymous Monk on Sep 13, 2013 at 08:30 UTC
Re: Loop that creates multidimensional array?
by Marshall (Canon) on Sep 13, 2013 at 12:59 UTC
    I am not sure of the objective.
    In general, an SQL query should get the result.
    There is probably some syntax error below, but
    this is the general idea.

    while ( ($dumb, $guru) = ( ($sth->fetchrow)[0,4]) ) { #do something here....with $dumb and $guru... #maybe push [$dumb,$guru] to another array? #but that could be just another SQL query. }