in reply to Re: Sorting multi-hash values
in thread Sorting multi-hash values

I believe this is very close but the problem now is my new hash, "$value_hash", can only hold one unique key. Therefore if I have list of values like:
colour device value ---------------------- red bike 1000 red shoes 1000 blue car 4 black plane 6 blue boat 1000 pink shoes 5 red car 5
The $value_hash will only have the following list:
colour device value ------------------------ blue boat 1000 black plane 6 red car 5 blue car 4
A hashes' keys must be unique, otherwise they get over written with the most recent entry. To prevent losing data, incase I have count values which are duplicates, I recreate a new hash with 3 keys and a value. The keys are "value", "colour", and "device". Creating New Hash "value_hash" (2nd hash):
foreach my $colour ( keys %hash) { foreach my $device ( keys %{$hash{$colour}}) { my $val = $hash{$colour}{$device}[0]; $value_hash{$val}{$colour}{$device}[0]++; } }
After that I then print the key (value) in order:
for my $value (sort {$b <=> $a} keys %value_hash) { for my $colour ( keys %{$value_hash{$value}}) { for my $os ( keys %{$value_hash {$value}{$colour}}) { printf("\n%-55s %-50s %-10s", $colour, $os, $value); } } }
Which now produces table...
colour device value ---------------------- red bike 1000 red shoes 1000 blue boat 1000 black plane 6 pink shoes 5 red car 5 blue car 4
Thank you for pointing me in the right direction monks...Papai