On 08/21/2012 05:33 PM, Eduardo wrote:
On 21/08/12 22:05, Chris Stinemetz wrote:
Hello List,
I am trying to sort a hash of arrays ( example below: )
I would the sort to sort in ascending order the first index of the array
then the second index of the array.
So in this example the arrays would sort to:
97,2,120,65
219,1,30,33
280,3,230,90
462,2,270,65
$VAR1 = {
'462-2' => [
'462',
'2',
'270',
'65'
],
'219-1' => [
'219',
'1',
'30',
'33'
],
'280-3' => [
'280',
'3',
'230',
'90'
],
'97-2' => [
'97',
'2',
'120',
'65'
};
Thanks in advance,
Chris
Hi, test this:
sub sorted
{
my ( $naa, $nab ) = $a =~ m|^(\d+)-(\d+)| && ( $1, $2 );
my ( $nba, $nbb ) = $b =~ m|^(\d+)-(\d+)| && ( $1, $2 );
return $naa <=> $nba unless $naa == $nba;
return $nab <=> $nbb;
since <=> will return 0 (false) if they are == you can just do this:
return $naa <=> $nba || $nab <=> $nbb;
but that is a lot of coding to run for each sort comparison. check out
Sort::Maker for ways to do that with much less effort and faster as well.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/