如何在 Perl 中删除符合特定条件的列?

问题描述 投票:0回答:1

我有下表

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
1个回答
0
投票

输出过滤表:

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
  • 将每一行拆分为列并存储
  • 如果某列的值!= 1,则需要它(不检查第一行)
  • 处理完所有行后,打印每行想要的列
© www.soinside.com 2019 - 2024. All rights reserved.