tags:

views:

92

answers:

3

I'm working with perl, and using DBI. Up to now, I've been using ->fetchall_arrayref to get the results of a database query, and just accessing the array by numeric keys. However, I much prefer to be able to access records by the field names (associative fetch) than numeric.

How do I do this, and what is the correct syntax for accessing the keys?

I would prefer something like:

$data[0]['name']

Instead of:

$data[0][1]

Working Solution

my %data;
@{$data{$id}}{('name')} = 'something';
+5  A: 

Read the DBI docs. Particularly, fetchall_hashref.

And you should also learn Perl syntax, as it's not the same as PHP.

Joe
@Joe I was looking for specific help please and thanks, not a RTFM answer. I took a look, but wasn't making a lot of sense. I know perl != PHP
Ben Dauphinee
If the size of the result set is not known, it would be safer to use `fetchrow_hashref` in a `while` loop. Further, assuming one wisely avoids using `SELECT * FROM` and limits the query to columns of interest only, it would be more efficient to use `fetchrow_arrayref` and assign to a small number of local variables.
Sinan Ünür
The DBI documentation is huge, but it really is beneficial to at least skim over it, so you're aware of what functionality exists.
Ether
@Sinan -- you're right, fetchall_* isn't the best way to go for large datasets ... I made a (possibly bad) assumption that if he was using fetchall_arrayref that fetchall_hashref was the change he was looking for (and would meet his requested structure).
Joe
@Joe: Your answer was correct and to the point in the context of the original question. I was just providing some complementary information.
Sinan Ünür
+2  A: 

If you do fetchall_hashref() then you get the hash you are looking for. The keys will be the field names from the database. I am a little late, and Joe got it, but it will be.

$data->{0}->{'field'};
plor
@plor Thanks for the good example. The manual info I read makes a lot more sense now.
Ben Dauphinee
@Ben When you strip away the noise, this answer boils down to stating the same information as @Joe did (without a link to the documentation) and the single line of code `$data->{0}->{'field'}` which remains unexplained and therefore is a **bad** example. For example, what is **`0`**? I know what it is, but do you? Since you seem to be averse to reading the documentation, I am going to go out on a limb and hypothesize that you don't.
Sinan Ünür
@Sinan you might want to go a little easy on Ben, he said he looked at the documentation but couldn't make sense of it. I was just taking his line of code which was php style and fixing it to perl style. I figured since he had 0 he knew the context. I'm sorry if it doesn't live up to your expectations.
plor
You can simplify your answer quite a bit: `$data->{0}{field};`. This works because the second arrow is not necessary, the after the first `[]` or `{}`, the dereference is implicit. Also, hash keys are quoted when they are single words.
daotoad
+4  A: 

You can use selectall_arrayref for this. Here's example from the DBI manpage:

You may often want to fetch an array of rows where each row is stored as a hash. That can be done simple using:

  my $emps = $dbh->selectall_arrayref(
      "SELECT ename FROM emp ORDER BY ename",
      { Slice => {} }
  );
  foreach my $emp ( @$emps ) {
      print "Employee: $emp->{ename}\n";
  }
eugene y