我有下表
head v1 v2 v3 v4 v5 v6
stn2 1 4 1 1 4 2
stn2 1 4 1 1 4 2
stn3 1 4 1 1 4 2
stn4 1 4 1 1 4 3
stn4 1 4 1 1 4 2
stn5 1 4 1 1 4 4
stn6 1 3 1 1 4 3
stn7 4 4 1 1 4 4
stn8 4 4 1 1 4 3
stn9 2 4 1 1 4 3
我想删除所有仅包含 1 的列。我已使用以下冗长的代码将六列放入数组中;
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
open(my $RSKF, " < ../risks.txt") || die "open risks.txt: failed $! ($^E)";
my $line;
my $count;
my @one_column;
my @two_column;
my @three_column;
my @four_column;
my @five_column;
my @six_column;
my $one_column;
my $two_column;
my $three_column;
my $four_column;
my $five_column;
my $six_column;
my @remove;
my @keep1;
my $keepcount=0;
while($line = <$RSKF>){
push(@one_column, (split(/\s+/, $line))[2]);
push(@two_column, (split(/\s+/, $line))[3]);
push(@three_column, (split(/\s+/, $line))[4]);
push(@four_column, (split(/\s+/, $line))[5]);
push(@five_column, (split(/\s+/, $line))[6]);
push(@six_column, (split(/\s+/, $line))[7]);
}
我尝试循环下面第四列不起作用。
$count=0;
for (my $i=1; $i < @four_column; ++$i){
if($four_column[$i] ge '2'){
$count++;
}
if($count > 0){
@four_column=@keep1;
$keepcount++;
}
else{
@four_column=@remove;
}
}
当然应该有一种更简单的方法来做到这一点。请帮忙。
输出过滤表:
perl -lae '
push @rows, [@F];
next if $.==1;
for (keys @F) {
$wanted[$_] = 1 if $F[$_]!=1;
}
END {
@cols = grep {$wanted[$_]} keys @F;
for (@rows) {
print join "\t", @$_[@cols];
}
}
' original.txt >new.txt