Search code examples
arraysperlsortinghashnumerical

Perl: Numerical sort of arrays in a hash


I have a hash of arrays, and I need to sort it first on the keys, and then on the values in the array.

Here's my simple code:

my %myhash;
$line1 = "col1 0.999";
$line2 = "col2 0.899";
$line3 = "col2 -0.52";
$line4 = "col2 1.52";

#insert into hash
@cols = split(" ", $line1);
push @{ $myhash{$cols[0]} }, $line1;
@cols = split(" ", $line2);
push @{ $myhash{$cols[0]} }, $line2;
@cols = split(" ", $line3);
push @{ $myhash{$cols[0]} }, $line3;
@cols = split(" ", $line4);
push @{ $myhash{$cols[0]} }, $line4;

foreach $k (sort {$a <=> $b} (keys %myhash)) {
   foreach $v(sort {$a <=> $b}(@{$myhash{$k}}))
   {
       print $k." : $v \n";     
   }
}

But I get the following output:

col1 : col1 0.999
col2 : col2 0.899
col2 : col2 -0.52
col2 : col2 1.52

So the keys are sorted fine, but the values aren't. I need them to come out like this:

col1 : col1 0.999
col2 : col2 -0.52
col2 : col2 0.899
col2 : col2 1.52

What's wrong with my code?


Solution

  • Not sure why you're building a hash. All you need is a quick Schwartzian Transform.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $line1 = "col1 0.999";
    my $line2 = "col2 0.899";
    my $line3 = "col2 -0.52";
    my $line4 = "col2 1.52";
    
    my @sorted = map { join ' ', @$_ }
                 sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] }
                 map { [ split ] } ($line1, $line2, $line3, $line4);
    
    print "$_\n" for @sorted;
    

    Also, having variables called $lineX is a bit of a red flag. You should probably store those values in an array.