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
In reply to Newbie hash/sorting question by msensay
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |