msensay has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks. Can someone explain why this works:
use strict; use warnings; my @data; push @data, { name => "Item A", price => 9.99 }; push @data, { name => "Item B", price => 4.99 }; push @data, { name => "Item C", price => 7.5}; my @sorted = sort {$a->{price} <=> $b->{price}} @data; print join "\n", map {$_->{name}." - ".$_->{price}} @sorted;
and this doesn't:
use strict; use warnings; my @data; my %recordset; $recordset{name} = "Item A"; $recordset{price} = 9.99; push @data, \%recordset; $recordset{name} = "Item B"; $recordset{price} = 4.99; push @data, \%recordset; $recordset{name} = "Item C"; $recordset{price} = 7.5; push @data, \%recordset; my @sorted = sort {$a->{price} <=> $b->{price}} @data; print join "\n", map {$_->{name}." - ".$_->{price}} @sorted;
the first one outputs this:
Item B - 4.99
Item C - 7.5
Item A - 9.99
And the second outputs this:
Item C - 7.5
Item C - 7.5
Item C - 7.5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie hash/sorting question
by Eliya (Vicar) on Dec 09, 2011 at 03:39 UTC | |
|
Re: Newbie hash/sorting question
by NetWallah (Canon) on Dec 09, 2011 at 04:07 UTC | |
by Anonymous Monk on Dec 09, 2011 at 12:07 UTC | |
by msensay (Novice) on Dec 09, 2011 at 14:21 UTC | |
by aaron_baugher (Curate) on Dec 09, 2011 at 17:06 UTC | |
by msensay (Novice) on Dec 09, 2011 at 19:59 UTC | |
|
Re: Newbie hash/sorting question
by RichardK (Parson) on Dec 09, 2011 at 12:03 UTC | |
|
Re: Newbie hash/sorting question
by pvaldes (Chaplain) on Dec 09, 2011 at 09:54 UTC |