The "fetch*" methods family mostly about returning a row, one at a time. The "*array_ref" indicates that a row will be returned as an array reference, so virtually you get ['value', 'for', 'each', 'column'] instead of ('value', 'for', 'each', 'column'). The "all" part indicates that all that array references will be returned, representing all results match the query. And the whole is bundled in another array reference. So you end up with AoA:
$all_rows = [ [R1C1, R1C2, ..., R1Cn], [R2C1, R2C2, ..., R2Cn], ..., [RnC1, RnC2, ..., RnCn], ];
Doesn't DWIW! If I do this:
...

that is, I've got an AoA -- I just want ... an A!

Well, AFAIK, Perl is more DWIM rather than DWIW. You may W an A, but by using fetchall_arrayref, you really M AoA. If you really M an A and you only want a single column from each row, then you can use selectcol_arrayref instead.
# Table: group # Columns: id, name # Values: 1, Admin # 2, User # 3, Public my $sql = 'select * from group'; my $r = $dbh->selectcol_arrayref($sql); # default to first column # or, if you want the the 'name' column my @r = @{$dbh->selectcol_arrayref($sql, {Columns=>[2]})}; # we want t +he 2nd column # if you know the column name you want in advanced: my $sql2 = 'select name from group'; my @group_names = @{$dbh->selectcol_arrayref($sql2)}; print Data::Dumper->Dump( [$r, \@r, \@group_names], [qw(id name group_names)], );
Output with Data::Dumper:
$id = [ '1', '2', '3', ]; $name = [ 'Admin', 'User', 'Public', ]; $group_names = [ 'Admin', 'User', 'Public', ];

Update: (09-07-2007) Fixed typo, corrected syntax to Data::Dumper->Dump() (was Data::Dumper(...)). I know I should just copy/paste instead of retyping. *sighs*


Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re^3: Why a reference, not a hash/array? by naikonta
in thread Why a reference, not a hash/array? by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.