如何在Perl中对并行数组进行排序? [英] How do you sort parallel arrays in Perl?

查看:192
本文介绍了如何在Perl中对并行数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个相同长度的数组.我想对第一个数组进行排序,并相应地对所有其他数组进行排序".例如,如果第一个数组是(7,2,9),第二个数组是("seven","two","nine"),第三个数组是("VII","II","IX"),则排序后(根据第一个数组值)将分别为(2,7,9)("two","seven","nine")("II","VII","IX").

I have a few arrays of the same length. I want to sort the first array, and make all the others array "sort" accordingly. For example, if the first array is (7,2,9) the second is ("seven","two","nine") and the third is ("VII","II","IX") after the sort (ascendingly according to the first array values) we will have (2,7,9) ("two","seven","nine") and ("II","VII","IX").

我该怎么做?

推荐答案

将数据重新组织到单个数组中以进行排序:

Re-organize the data to a single array for sorting:

my @a = ([7, "seven", "VII"], [2, "two", "II"], ..);
@a = sort { $a->[0] <=> $b->[0] } @a;

然后重新创建原始数组:

Then recreate the original arrays:

my(@a1, @a2, @a3);

for (@a) {
    push @a1, shift @$_;
    push @a2, shift @$_;
    push @a3, shift @$_;
}  

这篇关于如何在Perl中对并行数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆