如何排序列Perl中的数组或表? [英] How to sort an array or table by column in perl?

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

问题描述

我一直在到处寻找一个答案,我只是无法得到它的工作。

I've been looking everywhere for an answer to this, and I just can't get it to work.

我有被读入用perl数组的输入文件。该文件是含有表文本文件。 Perl的读取它作为一个阵列,其中每个元素是一个全行(包括所有五列)。这是该数组样子:

I have an input file that is read into an array using perl. The file is text file containing a table. Perl reads it in as an array, with each element being a full line (including all five columns). This is what the array looks like:

0__len__340   16    324    0    0.0470588235294118
1__len__251   2     249    0    0.00796812749003984
2__len__497   0     497    0    0
3__len__55    7     48     0    0.127272727272727
4__len__171   0     171    0    0
5__len__75    0     75     0    0
6__len__160   75    85     0    0.46875
7__len__285   1     284    0    0.00350877192982456
8__len__94    44    50     0    0.468085106382979

我需要通过降序排列最后一列这个表进行排序。所以,我的输出应该是:

I need to sort this table by the last column in descending order. So my output should be:

6__len__160   75    85     0    0.46875
8__len__94    44    50     0    0.468085106382979
3__len__55    7     48     0    0.127272727272727
0__len__340   16    324    0    0.0470588235294118
1__len__251   2     249    0    0.00796812749003984
7__len__285   1     284    0    0.00350877192982456
2__len__497   0     497    0    0
4__len__171   0     171    0    0
5__len__75    0     75     0    0

我已经尝试了一些办法,但都没有奏效。这里的code我试过:

I've tried a few approaches, but none have worked. Here's the code I've tried:

@input = <FILENAME>;
#Close the file
close FILENAME;
my @fractions;
my $y = 0;
for (my $x = 1; $x <= $#input; ++$x) {
    $fractions[$y] = (split (/\s/,$input[$x]))[4];
    ++$y;
}
my @sorted = sort {$b <=> $a} @fractions;
my $e = 1;
my $z = 0;
my $f = 0;
my @final;
 do {
    do {
        if ((split (/\s/,$input[$e]))[4] == $sorted[$z]){
            $final[$f] = $input[$e];
            ++$e;
            ++$f;
        } else { 
            ++$e;
        }
    } until ($e > $#input); 
    do {
        ++$z;
    } until ($sorted[$z] != $sorted[$z - 1]);
$e = 0;
} until ($z > $#sorted);
for (my $h = 0; $h <= $#final; ++$h) {
    print $final[$h] . "\n\n";

}

通过这个我基本上是试图把5列数字到各自为阵,对它们进行排序,然后回去通过原始数组,然后拉出排序数组相匹配的元素,并把它们放到最后的阵列。如果我一直做这个工作,这可能会工作,但它需要很长时间来运行,它是不切实际的。这个小桌子我使用测试我的code用了很长的时间,它运行,而一旦code工作将被处理的有几百万行的表。

With this one I basically tried to put the column 5 numbers into their own array, sort them, and then go back through the original array and pull out the elements that match the sorted array, and put them into the final array. This may work if I keep working on it, but it takes so long to run that it's impractical. This small table I'm using to test my code with took a long time for this to run, and once the code is working it will be dealing with a table that has millions of rows.

我也试过将sort命令表本身,而是我的输出是完全一样的表作为我的投入......它不会排序。

I also tried applying the sort command to the table itself, but my output is the exact same table as my input...it doesn't get sorted.

@input = <FILENAME>;
close FILENAME;
my @sorted = sort { $b->[4] <=> $a->[4] } @input;
for (my $h = 0; $h <= $#sorted; ++$h) {
    print $sorted[$h] . "\n\n";
}
exit;

最后,我试图把该阵列为哈希其中键是第一四列,由于第一列名是唯一的,并且这些值被第五列。然后,我希望我可以排序的值和键散列将留在他们的赋值。我不能让这要么工作,但不幸的是,几天前,我抹去了code。一个问题是,我无法弄清楚如何仅列第五前的字符串分割,所以我结束了两个字符串,一个包含前四列和一个含有五分之一。

Lastly, I tried to put the array into a hash where the key was the first four columns, since the first column name is unique, and the values being the fifth column. Then I hoped I could sort the hash by the values and the keys would stay with their assigned values. I couldn't get this to work either, though unfortunately it was a couple days ago and I erased the code. One problem was that I couldn't figure out how to split the string only before the fifth column, so I end up with two strings, one containing the first four columns and one containing the fifth.

任何人谁可以告诉我我在做什么毛病sort命令或者谁可能对如何做到这一个更好的主意,我真的AP preciate的帮助!

Anyone who could show me what I'm doing wrong with the sort command or who might have a better idea of how to do this, I'd really appreciate the help!

推荐答案

在你最后code例如,你可以替换

In your last code example you can replace

my @sorted = sort { $b->[4] <=> $a->[4] } @input;

my @sorted = sort { (split(' ', $b))[4] <=> (split(' ', $a))[4] } @input;

甚至

my @sorted = sort { (split(/\s+/, $b))[4] <=> (split(/\s+/, $a))[4] } @input;

如果输入数据与前导空格没有行。

if input data has no lines with leading spaces.

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

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