就行拆分成多列,並將其與上一行中的值進行比較。如果前一個值大於當前行中的值,則該列未排序。
#! /usr/bin/perl
use strict;
use warnings;
my @sorted = (1, 1, 1, 1);
my $first = <>; # read the first line
my @prev = split(/\t/, $first);
while (<>) {
my @cols = split(/\t/);
for (my $i = 0; $i < 4; ++$i) {
$sorted[$i] = 0 if ($prev[$i] gt $cols[$i]);
}
@prev = @cols;
}
for (my $i = 0; $i < 4; ++$i) {
my $not = $sorted[$i] ? '' : 'not ';
print "Column $i is $not sorted\n";
}
測試file.txt的
a a a a
b b b b
c c c c
d d d d
e e e a
f d f f
g g g g
呼叫作爲
perl script.pl file.txt
會給你
列0被分配
第1列未排序
列2被分揀
第3列未排序
該文本方式和試驗比較了按升序排列。如果您需要其他訂單或不同的比較,則必須相應地調整內部for循環。
排序什麼?整型?話?聽起來像一個棘手的perl/bash/awk單線程。我會用python來做,更易於維護和擴展。 –
是的,你可以寫一個簡短的(<15行,非常粗略的估計)perl腳本來做到這一點,如果這就是你的意思是「快速」。但既然你已經建議perl,我想你知道這件事。 – Khaur
我剛剛更新了問題的列類型細節 – Watt