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

Hello fellow monks!
I've been toying with my script for over an hour and am really struggling here. I think its an easy one, but all of my attempts have been futile. So here's my line of code: while ( @ratings = $ratings->fetchrow_array() ) { push @Array, ([$ratings[0], $ratings[1]]); } @ratings just holds two columns of data. I would like to be able to access this list once its done instead for dumping each row line by line to a file and then reading back in. I tryed print "$Array[0][0]\n"; to get the first value but I got a "]" instead?? Any help would be greatly appreciated as always!

bW

P.S. I've tested my data and I know I'm getting it from the database OK.

Replies are listed 'Best First'.
Re: Array of Arrays & Fetchrow
by robartes (Priest) on Oct 31, 2002 at 23:08 UTC
    The simplest way to do what you want to do is using fetchrow_arrayref (assuming you're using DBI here and $ratings is a statement handle):
    push @rows, $_ for $ratings->fetchrow_arrayref(); # @rows now contains references to rows. # You access rows and colums using: my $value=$rows[1]->[1]; # This would fetch the 2nd column of the 2nd row
    FWIW, to get your code to work, drop some parentheses:
    while ( @ratings = $ratings->fetchrow_array() ) { push @Array, [$rati ++ngs[0], $ratings[1]]; }

    CU
    Robartes-

      Is there any reason why one couldn't use fetchall_arrayref?
      $rows = $ratings->fetchall_arrayref;


      --
      Rock is dead. Long live paper and scissors!
      Your right Robartes, your way is probably easier. Thanks for such a detailed explaination of your code, I only really used fetchrow_array until now, because I'm not too confortable with references.

      Where there's a Willman, there's a way!

        I may have spoke to soon. I used robartes code:

        push @rows, $_ for $ratings->fetchrow_arrayref(); print XML "$rows[0]->[0]:$rows[0]->[1]:$rows[1]->[0]:$rows[1]->[1]\n";
        And my output is 1111:1::

        So I added a: push @rows, $_ for $ratings->fetchrow_arrayref(); This is to get the second line from the DB.

        And my output is 2222:2:2222:2 instead of 1111:1:2222:2 like I thought it would be. Can anybody tell me what I'm doing wrong here?
        Thanks

        BW

Re: Array of Arrays & Fetchrow
by rdfield (Priest) on Nov 01, 2002 at 10:13 UTC
    while ( my @ratings = $ratings->fetchrow_array() ) { push @Array, ([$ratings[0], $ratings[1]]); }

    rdfield