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}
}