Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hashes out of arrays in Perl?

I want to write a little "DBQuery" function in perl so I can have one-liners which send an SQL statement and receive back and an array of hashes, i.e. a recordset. However, I'm running into an issue with Perl syntax (and probably some odd pointer/reference issue) which is preventing me from packing out the information from the hash that I'm getting from the database. The sample code below demonstrates the issue.

I can get the data "Jim" out of a hash inside an array with this syntax:

print $records[$index]{'firstName'}

returns "Jim"

but if I copy the hash record in the array to its own hash variable first, then I strangely can't access the data anymore in that hash:


    %row = $records[$index];
    $row{'firstName'};

returns "" (blank)

Here is the full sample code showing the problem. Any help is appreciated:


my @records = (
   {'id' => 1, 'firstName' => 'Jim'},
   {'id' => 2, 'firstName' => 'Joe'}
);
my @records2 = ();

$numberOfRecords = scalar(@records);
print "number of records: " . $numberOfRecords . "\n";
for(my $index=0; $index < $numberOfRecords; $index++) {

   #works
   print 'you can print the records like this: ' . $records[$index]{'firstName'} . "\n";

   #does NOT work
   %row = $records[$index];
   print 'but not like this: ' . $row{'firstName'} . "\n";

} 
like image 344
Edward Tanguay Avatar asked Sep 09 '08 03:09

Edward Tanguay


People also ask

How do I print an array of hashes in Perl?

If you push a hash into an array, it's just a flat list. You're using the right dereference operators in your loop, but you're missing a backslash \ when pushing. push @holding, \%data; The backslash \ gives you a reference to %data , which is a scalar value.

Can Perl hash value array?

Elements of hash can be anything, including references to array.

How do you handle an array and hash in Perl?

A Perl hash is defined by key-value pairs. Perl stores elements of a hash in such an optimal way that you can look up its values based on keys very fast. Like a scalar or an array variable, a hash variable has its own prefix. A hash variable must begin with a percent sign (%).


1 Answers

The nested data structure contains a hash reference, not a hash.

# Will work (the -> dereferences the reference)
$row = $records[$index];
print "This will work: ", $row->{firstName}, "\n";

# This will also work, by promoting the hash reference into a hash
%row = %{ $records[$index] };
print "This will work: ", $row{firstName}, "\n";

If you're ever presented with a deep Perl data structure, you may profit from printing it using Data::Dumper to print it into human-readable (and Perl-parsable) form.

like image 98
Commodore Jaeger Avatar answered Sep 23 '22 08:09

Commodore Jaeger