Perl: Numerical sort of arrays in a hash

♀尐吖头ヾ 提交于 2020-02-04 04:14:25

问题


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?


回答1:


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.




回答2:


Are you sure you want the cols string in the values again? If not, try this:

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]} }, $cols[1];
@cols = split(" ", $line2);
push @{ $myhash{$cols[0]} }, $cols[1];
@cols = split(" ", $line3);
push @{ $myhash{$cols[0]} }, $cols[1];
@cols = split(" ", $line4);
push @{ $myhash{$cols[0]} }, $cols[1];

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

Else, write the sort function for the second foreach to ignore the 'cols' word, and only use the second word for sorting.

Edit:

Well I wanted to evade writing that myself, but since you asked ;) this explains the gist:

foreach $k (sort {$a <=> $b} (keys %myhash)) {
   foreach $v(sort mysorter (@{$myhash{$k}})) #mysorter is a sub, defined further on
   {
       print $k." : $v \n";
   }
}

sub mysorter {
  my $c = $a;
  my $d = $b;

  $c =~ s/(.*) (.*)/\2/gi;
  $d =~ s/(.*) (.*)/\2/gi;

  return $c <=> $d;
}


来源:https://stackoverflow.com/questions/7914931/perl-numerical-sort-of-arrays-in-a-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!