Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am trying to make a data structure, instead of array of arrays I need to be arrays of hashes, but I can not get it to work, here is what I have:
!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = (); ... foreach my $data ( @{ $sqldata } ) { # this is what I don't need anymore =code push @array, [ $data->{'code'}, $data->{'account'}, $data->{'name'}, $data->{'date'}, $data->{'street'} ]; =cut # I need a hash here my %rows; push @array, [ $rows{code} = $data->{'code'}; $rows{account} = $data->{'account'}; $rows{name} = $data->{'name'} ; $rows{date} = $data->{'date'}; $rows{street} = $data->{'street'}; ]; } print Dumper \@array;
thanks for the Help!

Replies are listed 'Best First'.
Re: Data into Array of Hashes
by toolic (Bishop) on Oct 23, 2015 at 17:17 UTC
      Used "{}" instead of "[]", now I am getting this error:
      "$data" requires explicit package name at... "%rows" requires explicit package name at...
        Please show the code that is giving you these errors. It is quite probably easy to fix, but it's difficult to say where the errors are without seeing the updated code.
Re: Data into Array of Hashes
by stevieb (Canon) on Oct 23, 2015 at 17:26 UTC

    This should do what you want, while eliminating the need to manually map out the new hash reference. Just add or remove any keys from the @wanted array.

    my @wanted = qw(code name date account street); my @array; foreach my $data (@{ $sqldata }) { push @array, { map {$_ => $data->{$_}} @wanted }; } print Dumper \@array;