如何在Perl中按列对数组或表排序? [英] How can I sort an array or table by column in Perl?

查看:162
本文介绍了如何在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 a 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

我尝试了几种方法,但是都没有奏效.这是我尝试过的代码:

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列数字放入它们自己的数组中,对它们进行排序,然后返回原始数组并取出与排序后的数组匹配的元素,然后将它们放入最终数组中

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;

最后,由于第一列的名称是唯一的,而值是第五列,所以我试图将数组放入一个哈希,其中键是前四列.

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命令在做什么?有更好的方法吗?

What am I doing wrong with the sort command? Is there a better way to do this?

推荐答案

在您的上一个代码示例中,您可以替换

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天全站免登陆