Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Loop that creates multidimensional array?

by jdlev (Scribe)
on Sep 12, 2013 at 16:53 UTC ( #1053766=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Loop that creates multidimensional array?
Download Code
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 (Vicar) 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 (Prior) 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. }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1053766]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-08-02 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls