laziness, impatience, and hubris 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??

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}; [download] You can also chain together the dereferencing like this:  my$value = $array_ref->[2]->{$key};
[download]

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

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

Create A New User
Node Status?
node history
Node Type: note [id://1040346]
help
Chatterbox?
 [choroba]: needed everywhere, available rarely [LanX]: Question: starting a script from an icon in Windows, but after C-c the window closes ... what I want is to restart in the cmd.exe. Recommendations? [Corion]: LanX: What do you mean by "restart in the cmd.exe" ? [Corion]: Do you want to launch a script and keep the command prompt/console window open? [Corion]: Do you want to wait for a key press before closing the window? [LanX]: I want the command line in the history [tye]: -Mouse [Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file. [LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy [Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-03-27 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (320 votes). Check out past polls.