[英]Sort Hash Value In A First Element Manner Perl
I have a hash that I want to sort its values in first found element manner. 我有一个哈希,我想以首先找到的元素的方式对其值进行排序。 From below script, I sorted out the values in alphabetical manner, but what I really want is to sort it according to what values comes first, for example, key 0 with the values of ACBAD, the first value is A, so it sort all A first, then C, and then B and lastly D, I know how to sort it alphabetically but I can't figure out how to do with my wanted order. 在下面的脚本中,我按字母顺序对值进行了排序,但我真正想要的是根据先出现的值对它进行排序,例如,键0为ACBAD的值,第一个为A,因此对所有值进行排序首先,然后是C,然后是B,最后是D,我知道如何按字母顺序对其进行排序,但我不知道如何处理所需的订单。
#!/usr/bin/perl
use warnings;
use strict;
use List::MoreUtils;
use Tie::IxHash;
my %KEY_VALUE;
#tie %KEY_VALUE,'Tie::IxHash';
my %KEY_VALUE= (
0 => [ 'A', 'C', 'B', 'A' ,'D'],
5 => [ 'D', 'F', 'E', 'F', 'F','E'],
2 => [ 'Z', 'X', 'A', 'Y', 'X', 'Y', 'A' ],
4 => [ 'E', 'R', 'M' ,'M','E'],
3 => [ 'A', 'B', 'B', 'A' ],
1 => [ 'C', 'C', 'F', 'E', 'C', 'E'],
);
#Code from Schwern user:14660
# Iterate through the keys in numeric order.
for my $key (sort {$a <=> $b } keys %KEY_VALUE)
{
# Get the value
my $val = $KEY_VALUE{$key};
# Sort it in place #How to sort it in First Found Element instead of alphabetically increasing?
@$val = sort { $a cmp $b } @$val;
# Display it
print "$key -> @$val\n";
}
The wanted output is like this, and I want to print out the sorted keys and values. 所需的输出是这样的,我想打印出排序的键和值。
0 -> AACBD
1 -> CCCFEE
2 -> ZXXAAYY
3 -> AABB
4 -> EERMM
5 -> DFFFEE
If you view it as a sorting problem, you can build a mapping between the values and their desired order: 如果您将其视为排序问题,则可以在值及其所需顺序之间建立映射:
my @unsorted = qw( Z X X A Y X Y A );
my %order; @order{ reverse @unsorted } = reverse 0..$#unsorted; # Z=>0, X=>1, A=>3, Y=>4
my @sorted = sort { $order{$a} <=> $order{$b} } @unsorted; # ZXXXAAYY
But it's not actually sorting problem. 但这实际上不是排序问题。 Sorting is O(N log N), but this can be done in O(N). 排序为O(N log N),但这可以在O(N)中完成。
my @unsorted = qw( Z X X A Y X Y A );
my %counts;
my @uniques = grep { !$counts{$_}++ } @unsorted;
my @sorted = map { ($_) x $counts{$_} } @uniques; # ZXXXAAYY
You get what I think you want, is pretty simple. 您得到了我认为想要的东西,非常简单。 You just walk the hash in sorted key order and then dereference the array and sort that. 您只需按照排序的键顺序遍历哈希,然后取消引用数组并对其进行排序。
#!/usr/bin/perl
use warnings;
use strict;
my %KEY_VALUE= (
0 => [ 'A', 'C', 'B', 'A' ,'D'],
5 => [ 'D', 'F', 'E', 'F', 'F','E'],
2 => [ 'Z', 'X', 'A', 'Y', 'X', 'Y', 'A' ],
4 => [ 'E', 'R', 'M' ,'M','E'],
3 => [ 'A', 'B', 'B', 'A' ],
1 => [ 'C', 'C', 'F', 'E', 'C', 'E'],
);
for my $key (sort { $a <=> $b } keys %KEY_VALUE) {
print $key, " -> ", sort(@{ $KEY_VALUE{$key} }), "\n";
}
But that doesn't give the results you show. 但这并不能提供您显示的结果。 Please explain how each array should be sorted. 请解释每个数组应该如何排序。 I thought you wanted them sorted in alphabetical order, but that's not the case in your example. 我以为您希望它们按字母顺序排序,但是在您的示例中情况并非如此。
I get this: 我得到这个:
0 -> AABCD
1 -> CCCEEF
2 -> AAXXYYZ
3 -> AABB
4 -> EEMMR
5 -> DEEFFF
But you ask for: 但您要求:
0 -> AACBD
1 -> CCCFEE
2 -> ZXXAAYY
3 -> AABB
4 -> EERMM
5 -> DFFFEE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.