How to iterate through Array of Hashes in a Hash in Perl

淺唱寂寞╮ 提交于 2019-12-24 07:25:46

问题


I have an Array of Hashes in a Hash that looks like this:

$var  = {
      'items' => [
                      {
                        'name'  => 'name1',
                        'type'  => 'type1',
                        'width' => 'width1',
                      },
                      {
                        'name'  => 'name2',
                        'type'  => 'type2',
                        'width' => 'width2',
                      },
                      {
                        'name'  => 'name3',
                        'type'  => 'type3',
                        'width' => 'width3',
                      }                      
                   ]
    };

I wrote the following code to get the values from a file.

my @members = ("name"    =>  $name,
               "type"    =>  $type,
               "width"   =>  $width);

$rec->{$items} = [ @members ];

push @var, $rec;

I am not sure how to retrieve the values from this data structure.

I saw the solution in Iterate through Array of Hashes in a Hash in Perl. But I did not understand it. I am not sure what was $filelist that they had mentioned in the code.

foreach my $file (@{ $filelist{file} }) {
    print "path: $file->{pathname}; size: $file->{size}; ...\n";
}

I am new to perl and please provide me details in your reply.


回答1:


An excellent reference for data structures like the one you're dealing with is Perl's Data Structures Cookbook.

That said, here is the code:

for my $item (@{$aoh->{items}}) {
    # '@' casts the $aoh->{items} hash references to an array
    print $item->{name};
}



回答2:


First of all the structure in that question like

$VAR1 = {
          'file' => [
                      {
                        'pathname' => './out.log',
                        'size' => '51',
                        'name' => 'out.log',
                        'time' => '1345799296'
                      },
.
.
.
}

is actually print or output of the hashref $filelist. Data::Dumper module which helps in printing the structures like hashref, arrayref, etc. in a way that you can read properly.

So $VAR1 is nothing but $filelist printed using Dumper.

Now, about the foreach loop iterating through values:

foreach my $file (@{ $filelist{file} })

Here, the $filelist{file} part returns the array reference (Note: [, ] represents arrayref).

Now, when you use @{} on this arrayref i.e. @{ $filelist{file} }, this converts or expands as array.

Once we get the arrayref converted to array type, we can iterate using foreach.

Please note that when you use $hashrefname->{$key}, it means hashref accessing the key, $hashname{$key} means hash accessing key. Same is for arrayef and array, but instead of keys there are numbers to acess in case of array.

Solution for your problem:

You need to store the members as hashref and not array i.e.

my $member = {"name"    =>  $name,
               "type"    =>  $type,
               "width"   =>  $width};

Then you can push this each hashref that you read from file(i am guessing it is from file) into the array

push @arr, $member

And then assign the arrayref to the items

$rec->{items} = \@arr

Now you can access values as

foreach my $eachhashref (@{$rec->{items}})
{
print $eachhashref->{name}
}


来源:https://stackoverflow.com/questions/22124033/how-to-iterate-through-array-of-hashes-in-a-hash-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!