What you actually have there is an array of hashes.
Since the delete function returns the item that was deleted, we can use it directly in the assignmnent:
use warnings; use strict; use Data::Dumper; my $aref = [ { 'name' => 'Tim', 'total' => 4, 'months' => 1, 'days' => 3 }, { 'total' => 22, 'name' => 'Adam', 'days' => 22 }, { 'name' => 'Keas', 'total' => 114, 'months' => 5, 'days' => 107, 'test' => 2 } ]; my $href; $href->{delete $_->{name}} = $_ for @$aref; print Dumper $href;
Output:
$VAR1 = { 'Adam' => { 'days' => 22, 'total' => 22 }, 'Keas' => { 'total' => 114, 'test' => 2, 'days' => 107, 'months' => 5 }, 'Tim' => { 'total' => 4, 'days' => 3, 'months' => 1 } };
In reply to Re: How to loop through hash of array and convert into hash with key and value.
by stevieb
in thread How to loop through hash of array and convert into hash with key and value.
by Sami_R
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |