如何比较perl中同一列中的不同行

时间:2014-02-07 02:08:13

标签: perl parsing

我有不同列的文件,我想先将它们解析为列,然后取一个特定列并比较该列中的不同行。这是我的示例输入文件

A02     2260333 2260527 Contig1000|m.2597       216     -
A02     2260222 2260254 Contig1000|m.2597       2       -
A02     2260333 2260528 Contig1000|m.2596       216     -
A02     2261298 2261445 Contig1000|m.2596       202     -
A02     2260845 2260895 Contig1000|m.2596       20      -
A06     1006786 1006986 Contig1002|m.2601       212     -

我到目前为止已经解析了该文件然后得到了列。现在我想获取id列并检查该id列中的不同行并进行比较以查看第一行是否相同。如果它是相同的那么我会做一些事情,如果不做其他事情。

到目前为止,我已经写了这个......

open(my $fh_in, "<", "test_parsing.bed") or die "Could not open file $!";

while(my $line = <$fh_in>) {
    chomp($line);
    my ($chr, $start, $end, $id, $map, $strand) = split ' ', $line;     
    print Dumper($id);

}   
close $fh_in;

这是我想要生成的输出文件....

A02      2260222 2260895 Contig1000 216 - 2260222 2260895 0 3 33,196,50 0,111,623

然后对id contig1000 | m.2596等进行同样的操作......

由于

众议员

1 个答案:

答案 0 :(得分:1)

我会写这样的东西

use strict;
use warnings;

open my $fh_in, '<', 'test_parsing.bed' or die "Could not open input file: $!";

my $first_id;

while (<$fh_in>) {
  my ($chr, $start, $end, $id, $map, $strand) = split;

  if (not defined $first_id) {
    $first_id = $id;
  }
  elsif ($id eq $first_id) {
    # Action in case ID matches first line
  }
  else {
    # Action in case ID differs from first line
  }
}