in reply to use of DBI perl function fetchall_arrayref

My standard idiom for this sort of thing goes like this:
my $rows = $sth->fetchall_arrayref; for my $row ( @$rows ) { my @fields = @$row; # do something with @fields... } # or, if the per-field activity is pretty dense: for my $row ( @$rows ) { for my $field ( @$row ) { # do something with $field... } }
No doubt there are other approaches that some would consider more "elegant" or "clever", but usually when dealing with basic query activity like this, the plain and simple loops with descriptive variable names is just the easiest way.

Replies are listed 'Best First'.
Re^2: use of DBI perl function fetchall_arrayref
by kmullin5016 (Acolyte) on Jan 26, 2007 at 14:56 UTC
    Ok, Here is what I've got:
    my $rows = $sth_tss->fetchall_arrayref; $count = 0; print $CGI->p("rows is ", @$rows); for my $row ( @$rows ) { $CGI->p("Row", ++$count); my @fields = @$row; for my $field ( $fields ) { print $CGI->p($field); } }

    and the code it generates is:

    ARRAY(0x40285608) ARRAY(0x402856a4) ARRAY(0x40285740) ARRAY(0x402 RRAY(0x40285878) ARRAY(0x402860e0) ARRAY(0x4028617c) ARRAY(0x40286218) + A 02862b4) ARRAY(0x40286350) ARRAY(0x402863ec) ARRAY(0x40286488) ARRAY(0 +x4 ARRAY(0x40287104) ARRAY(0x402871a0) ARRAY(0x4028723c) ARRAY(0x402872d +8) x40287374) ARRAY(0x40287fbc) ARRAY(0x40288058) ARRAY(0x402880f4) ARRAY +(0 0) ARRAY(0x4028822c) ARRAY(0x402882c8) ARRAY(0x40288364) ARRAY(0x40288 +f3 (0x40288fd8) ARRAY(0x40289074) ARRAY(0x40289110) ARRAY(0x402891ac) ARR +AY 248) ARRAY(0x40289e90) ARRAY(0x40289f2c) ARRAY(0x40289fc8) ARRAY(0x402 +8a AY(0x4028a100) ARRAY(0x4028a19c) ARRAY(0x4028ae14) ARRAY(0x4028aeb0) A +RR 8af4c) ARRAY(0x4028afe8) ARRAY(0x4028b084) ARRAY(0x4028b120) ARRAY(0x4 +02 RRAY(0x4028bddc) ARRAY(0x4028be78) ARRAY(0x4028bf14) ARRAY(0x4028bfb0) + A 028c04c) ARRAY(0x4028c0e8) ARRAY(0x4028d100) ARRAY(0x4028d19c)
    and so on. What am I doing wrong?
        I made the change, no effect, I'm still getting the same thing. Now, here is my code:
        my $rows = $sth_tss->fetchall_arrayref; # print Dumper $rows; $count = 0; print $CGI->p("rows is ", @{$rows}); for my $row ( @{$rows} ) { $CGI->p("Row", ++$count); my @fields = @{$row}; for my $field ( @fields ) { print $CGI->p($field); } }

        The commented out dumper worked well, I'm definitely getting the data, I just don't know how to access it. I even tried it with just

        my @fields = $row

        and that had no affect. Perl is as bad as C, if it is syntactically correct, it will do something, but not at all what you had in mind.

      You said that you ran this piece of code., Looking at it you have three print statements that do not appear to be printing out anything similiar to what you posted.

      For example each ARRAY reference should be between p() tags yet it is one long wrapped string.

      Are you sure you looking at the right location for this code?

      This line in your code:
      print $CGI->p("rows is ", @$rows);
      is telling perl to stringify a list of array references and print them out as strings to your web page. And that is exactly what you are seeing. Those "ARRAY(0x....)" things are perl's normal way of converting references into strings.

      Others have pointed other problems with the code, which you might have fixed. Do pay special attention to every "@" and "$", and to the (absence of) spaces around them -- perl is especially picky about those details, because it needs to be.