in reply to How to loop through hash of array and convert into hash with key and value.
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 } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to loop through hash of array and convert into hash with key and value.
by Sami_R (Sexton) on Dec 28, 2019 at 11:01 UTC |