如何在perl中比较同一列中的不同行
我有一个具有不同列的文件,我想先将它们解析为列,然后取一个特定的列并比较该列中的不同行。下面是我的示例输入文件如何在perl中比较同一列中的不同行,perl,parsing,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 2261
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;
然后对id Contig1000 | m.2596等执行相同的操作
谢谢
Upendra我会这样写
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
}
}
使用严格;
使用警告;
在中打开我的$fh_,'请给出您想要的输出示例
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
}
}