Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: How does DBI return an arrayref with key/value pairs?

by ruzam (Curate)
on Jun 22, 2013 at 03:02 UTC ( #1040243=note: print w/replies, xml ) Need Help??

in reply to How does DBI return an arrayref with key/value pairs?

As BillKSmith pointed out:

 { Slice => {} }

is your answer. The Slice tells DBI what format you want the selected records to be returned as. You've asked for them to be returned as hash references {} so that's what you get.

You could have also used:

 { Slice => [] }

in which case, each selected row would be returned as an array reference. You wouldn't get the table column names (hash keys), instead you would just get an array of the row values and it would be up to you to remember the order of the columns selected.

Returning the records as hash references is slower than returning the records as array references. But returning records as array references can make the code harder to read (and harder to find bugs). Especially if you select all columns generically using queries like "Select * From". Returning the records as array references could blow up on you horribly in this situation if the database table layout changes in the future. I avoid using "Slice => []" unless the number of columns selected is small or I'm expecting huge numbers of returned records and performance is critical.

Replies are listed 'Best First'.
Re^2: How does DBI return an arrayref with key/value pairs?
by Anonymous Monk on Jun 26, 2014 at 21:41 UTC
    All valuable info. I'd like to also add that dbh->selectall_arrayref( $Sql ) by default returns a Reference to an Array of References of each row fields stored in an Array, so it's essentially it's a Reference to an Array of References of row-Arrays, thus there is no need to pass extra parameter { Slice => [] }, since that's a default behavior. Only if you want your individual rows be stored in Hashes instead of Arrays do you need to pass { Slice => {} } as a second parameter, so your call would look like this: dbh->selectall_arrayref( $Sql, { Slice => {} } )

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040243]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-07-22 09:10 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (337 votes). Check out past polls.