我试图使用Perl对文本文件中的下列价格列进行排序。
Time Num Size Price Act | Act Price Size Num Time
11:30:12.957 1 3000 11.90 A | A 11.05 500 1 11:30:12.954
11:30:12.957 1 100 11.75 A | A 14.00 1676 3 11:30:12.957我可以将文本文件读取到数组中,并按行对其进行排序,但我想不出如何按升序或降序排序特定的列?试着一次读取文本文件中的一个元素,如下所示,然后尝试按降序排序第一个Price列
use strict;
use warnings;
open(my $file_handle, '<', 'Data.txt') or die("Error: File cannot be opend: $!");
my @words;
while (<$file_handle>) {
chomp;
@words = split(' ');
}发布于 2013-09-28 16:32:50
use strict;
use warnings;
open(my $file_handle, '<', 'Data.txt') or die("Error: File cannot be opend: $!");
my @rows;
while (<$file_handle>) {
$. > 1 or next; # skip header line
chomp;
push @rows, [ split ]; # split current line on \s+
}
# sort descending on 4-th column
@rows = sort { $b->[3] <=> $a->[3] } @rows;
# ascending sort on same column
# @rows = sort { $a->[3] <=> $b->[3] } @rows;https://stackoverflow.com/questions/19069071
复制相似问题