Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by grantm (Parson)
on Jun 24, 2013 at 01:59 UTC ( #1040346=note: print w/ replies, xml ) Need Help??


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

I think you'll find the enlightenment you're after in perlreftut - Mark's very short tutorial about references.

When you called $rows = $dbh->selectall_arrayref, DBI returned a single scalar value which you assigned to $rows. That value was a reference to an array. Each element in that array was a reference to a hash of column names and values.

The array-dereferencing syntax you quoted:

$ { $array_ref } [2]

is exactly equivalent to this syntax:

$array_ref->[2]

And in your case would return a hashref.

my $hash_ref = $array_ref->[2]; my $value = $hashref->{$key};

You can also chain together the dereferencing like this:

my $value = $array_ref->[2]->{$key};

And you can even omit the arrow when the initial data structure contains references:

my $value = $array_ref->[2]{$key};


Comment on Re: How does DBI return an arrayref with key/value pairs?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2015-07-05 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls