Perl:散列中数组的数字排序

发布于 2024-12-12 07:56:12 字数 886 浏览 0 评论 0原文

我有一个数组的哈希值,我需要首先对键进行排序,然后对数组中的值进行排序。

这是我的简单代码:

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

但我得到以下输出:

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

所以键排序良好,但值却没有。我需要他们这样说:

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

我的代码出了什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-12-19 07:56:12

不确定你为什么要构建哈希。您所需要的只是一个快速 Schwartzian变换

#!/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;

另外,拥有名为 $lineX 的变量也是一个危险信号。您可能应该将这些值存储在数组中。

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.

冰魂雪魄 2024-12-19 07:56:12

您确定要再次将 cols 字符串包含在值中吗?如果没有,请尝试以下操作:

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

否则,为第二个 foreach 编写排序函数以忽略“cols”单词,并仅使用第二个单词进行排序。

编辑:

嗯,我想避免自己写这个,但既然你问了;)这解释了要点:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文