I can sort and print this hash by the keys using the following:my %products ( 1 => { name => "Floor Wax", wholesale => "50.00", retail => "75.00", }, 2 => { name => "Paper Towel", wholesale => "20.00", retail => "40.00", }, 3 => { name => "Hand Soap", wholesale => "30.00", retail => "65.00", }, );
How would I sort %products by the name or wholesale of each product? I have tried many variations of this to no avail.foreach my $key (sort {$a <=> $b} keys %products) { print "<br>K|$key| N|$products{$key}{name}| W|$products{$key}{whol +esale}| R|$products{$key}{retail}|"; }
I have read the hash of hashes section of the camel many times. Am I missing something obvious here or should I be transforming this into another data structure before printing.foreach my $key (sort {$products{retail}{$a} <=> $products{retail}{$b} + keys %products) { print "<br>K|$key| N|$products{$key}{name}| W|$products{$key}{whol +esale}| R|$products{$key}{retail}|"; }
Originally posted as a Categorized Question.
In reply to How do I sort a Hash of Hashes? by greywolf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |