将数组值与哈希进行比较,如果在文件中匹配则删除键

发布于 2024-12-15 04:06:30 字数 849 浏览 0 评论 0原文

我有 3 列的 .txt 文件。我想将第一列与第二列进行比较,如果第一列中的值出现在第二列中,我想删除第二列和第三列中的该条目(不应修改第一列)。结果应存储在新文件中。

示例输入:

Col 1                  Col 2              Col 3
VIBHAR_02293_1   VIBHAR_00819_2     tatatattattata
VIBHAR_00819_2   VIBHAR_00819_4     tattavgaggagag
VIBHAR_00705_3   VIBHAR_00705_7     attaggaccaggat
VIBHAR_00819_4   VIBHAR_02153_9     ccagggattattat

示例输出:

VIBHAR_02293_1   VIBHAR_00705_7     attaggaccaggat
VIBHAR_00819_2   VIBHAR_02153_9     ccagggattattat
VIBHAR_00705_3   
VIBHAR_00819_4   

我尝试使用以下代码,但它不起作用:

while($line=(<File>))
{
chomp($line);
@F=split('\t',$line);
    $hash{$F[1]}=$F[2];
    if ($F[0] eq $F[1])
    {
#        print "$line\n";
         delete($hash{keys});
    }
}

如果我上面发布的列的格式不好,那么我想只有我的问题就足够了。

I have .txt file with 3 columns. I want to compare the first with the second column and if values from the first column appear in the second column, I want to delete that entry in the second and third column (first column should not be modified). The result should be stored in a new file.

Example input:

Col 1                  Col 2              Col 3
VIBHAR_02293_1   VIBHAR_00819_2     tatatattattata
VIBHAR_00819_2   VIBHAR_00819_4     tattavgaggagag
VIBHAR_00705_3   VIBHAR_00705_7     attaggaccaggat
VIBHAR_00819_4   VIBHAR_02153_9     ccagggattattat

Example output:

VIBHAR_02293_1   VIBHAR_00705_7     attaggaccaggat
VIBHAR_00819_2   VIBHAR_02153_9     ccagggattattat
VIBHAR_00705_3   
VIBHAR_00819_4   

I tried using following code but it did not work:

while($line=(<File>))
{
chomp($line);
@F=split('\t',$line);
    $hash{$F[1]}=$F[2];
    if ($F[0] eq $F[1])
    {
#        print "$line\n";
         delete($hash{keys});
    }
}

If the format of the columns which I posted above is not good then, only my question is enough I guess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-12-22 04:06:30
#!/usr/bin/perl
use warnings;
use strict;

my %H;
while (<>) {
    chomp;
    my @F = split /\t/;
    $H{$F[0]} = [$., $F[1], $F[2]];
}

my @col1;
my @col23;

for my $col1 (sort { $H{$a}[0] <=> $H{$b}[0] } keys %H) {
    push @col1, $col1;
    next if exists $H{ $H{$col1}[1] };
    push @col23, [@{ $H{$col1} }[1,2]];
}

for my $i (0 .. $#col1) {
    print $col1[$i];
    print "\t", join "\t", @{ $col23[$i] } if $i < @col23;
    print "\n";
}

您真的想“上移”第 2 列和第 3 列中的值吗?

#!/usr/bin/perl
use warnings;
use strict;

my %H;
while (<>) {
    chomp;
    my @F = split /\t/;
    $H{$F[0]} = [$., $F[1], $F[2]];
}

my @col1;
my @col23;

for my $col1 (sort { $H{$a}[0] <=> $H{$b}[0] } keys %H) {
    push @col1, $col1;
    next if exists $H{ $H{$col1}[1] };
    push @col23, [@{ $H{$col1} }[1,2]];
}

for my $i (0 .. $#col1) {
    print $col1[$i];
    print "\t", join "\t", @{ $col23[$i] } if $i < @col23;
    print "\n";
}

Do you really want to "move up" the values in columns 2 and 3?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文