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;
}


with this loop:

foreach ( sort sorted keys $hash )
{
   print "$_\n";
}


Eduardo.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to