greywolf has asked for the wisdom of the Perl Monks concerning the following question: ⭐ (sorting)
I have a hash of hashes that looks like this:
mr greywolf
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}|"; }
mr greywolf
Originally posted as a Categorized Question.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How do I sort a Hash of Hashes?⭐
by neuroball (Pilgrim) on Jan 10, 2004 at 02:34 UTC | |
by ysth (Canon) on Jan 11, 2004 at 07:27 UTC | |
Re: How do I sort a Hash of Hashes?⭐
by NetWallah (Canon) on Jan 10, 2004 at 05:55 UTC | |
Re: How do I sort a Hash of Hashes?
by injunjoel (Priest) on Oct 19, 2005 at 20:21 UTC | |
Re: How do I sort a Hash of Hashes?
by neuroball (Pilgrim) on Jan 10, 2004 at 02:23 UTC | |
Re: How do I sort a Hash of Hashes?
by Anonymous Monk on Jun 22, 2004 at 17:22 UTC |
Back to
Seekers of Perl Wisdom