title: How to get hashes out of arrays in Perl?
body: <p>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.</p>
  
  <p>I can get the data "Jim" out of a hash inside an array with this syntax:</p>
  
  <pre><code>print $records[$index]{'firstName'}
  </code></pre>
  
  <p>returns "Jim"</p>
  
  <p>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:</p>
  
  <pre><code>
      %row = $records[$index];
      $row{'firstName'};
  </code></pre>
  
  <p>returns "" (blank)</p>
  
  <p>Here is the full sample code showing the problem. Any help is appreciated:</p>
  
  <pre><code>
  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 &lt; $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";
  
  } 
  </code></pre>