http://www.perlmonks.org?node_id=1027724

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

Hi Monks!
I saw a similar code here that shows very close what I am going through. I need to add an extra element to the array ( id ), but no luck, may be someone here will help me with that.
Here is the code, sample code thanks for someone here, that shows what I am trying to do:
#/usr/bin/perl use strict; use warnings; use Data::Dumper; my $array = [ { 'name' => 'Discount', 'total' => '100 ', 'type' => 'Home', 'id' => '01', }, { 'name' => 'Documents', 'total' => '100 ', 'type' => 'Home', 'id' => '02', }, { 'name' => 'Money', 'total' => '340 ', 'type' => 'Card', 'id' => '03', }, { 'name' => 'State', 'total' => '40 ', 'type' => 'Box', 'id' => '04', }, { 'name' => 'Slice', 'total' => '30 ', 'type' => 'Box', 'id' => '05', }, { 'name' => 'Part', 'total' => '45 ', 'type' => 'Box', 'id' => '06', }, ]; my %hash; $hash{$_->{'type'}}{$_->{'name'}} = $_->{'total'} for @$array; #$hash{$_->{'type'}}{$_->{'name'}}{$_->{'total'}} = $_->{'id'} for @$a +rray; # doesnt work print "Results:\n"; for my $k1 (sort keys %hash) { print " $k1:\n"; for my $k2 (sort keys %{$hash{$k1}}) { print " $k2 - $hash{$k1}{$k2}\n"; } } print "\n\n"; warn Dumper %hash; # This is what I am trying to get out of it: =code Results: Box: id - 06 - Part - 45 id - 05 - Slice - 30 id - 04 - State - 40 Card: id - 03 - Money - 340 Home: id - 01 - Discount - 100 id - 02 - Documents - 100 =cut
Thanks for helping!Home