Perl - hashref

I’m using fetchrow_hashref to retrieve a db row, and I’d like to retain the data at that reference in another associative array:

while ($hash = $prep->fetchrow_hashref)
@keys = keys(???hash);???
foreach $Key (@keys) {$data{$Key} = $hash->{$Key}}

How do I de-reference the hash table pointer so I can assign the reference value to the actual value in my data array? I know I can just hard code each db field name but I was hoping to get around that using the keys function.

You can do that in just one simple statement:

while ($hash = $prep->fetchrow_hashref) { Úta = %$hash; }Yup, it’s that simple. However that would trash any values already in Úta.

The “keys” is a function that returns a list of the keys in the associative array. There is also a “values” function that returns the list of the values. So re-writing your code becomes:

while ($hash = $prep->fetchrow_hashref) { foreach $key (keys %$hash) { $data{$key} = $hash->{$key} } }Now, let’s say you want to make a list array of the rows returned, and you want each element to be a reference to an associative array.

while ($hash = $prep->fetchrow_hashref) { push(@rows, { %$hash }); }An anonymous associative array is created by de-referencing $hash - this makes a new copy of the associative array from $hash on each iteration. The braces then make a reference to this copy, and this new reference gets pushed on to the list array.

This works - the piece I needed was: (keys %$hash).
Thank you!