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

Re: Result set into array

by imp (Priest)
on Nov 19, 2006 at 02:41 UTC ( #584912=note: print w/ replies, xml ) Need Help??


in reply to Result set into array

Your question specified that you would like the results in an array, but the example output you provided is a hash, and you used selectall_hashref in your earlier reply.

If you would like to fetch all of the rows and have them in a hash then you need to specify which column should be used, as in this example:

my $hoh = $dbh->selectall_hashref($sql, $index_column);
In this example the structure would be as follows:
$hoh = { 1 => {id => 1, col2=>"abc", col3=>"def", col4=>"ghi"}, 2 => {id => 2, col2=>"lmn", col3=>"opq", col4=>"rst"}, }
Keep in mind that as it is returning a hashref the order of the rows will not be preserved. To preserve the order you would use selectall_arrayref as follows:
my $aoa = $dbh->selectall_arrayrefref($sql);
In this example the structure would be as follows:
$aoa = [ [1,"abc", "def", "ghi"], [2,"lmn", "opq", "rst"], ]
If you would like to preserve both the order and the column names, then you can use the Slice option for selectall_arrayref as follows:
my $aoh = $dbh->selectall_arrayref($sql, {Slice => {}});
In this example the structure would be as follows:
$aoh = [ {id => 1, col2=>"abc", col3=>"def", col4=>"ghi"}, {id => 2, col2=>"lmn", col3=>"opq", col4=>"rst"}, ]


Comment on Re: Result set into array
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://584912]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-28 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (190 votes), past polls