我有以下数据和perl代码。我试图找到每个标题下的最高分(最后一列)的字母组合(> dna1,> dna2)并打印出来。我在Perl中编写代码如下:
use strict;
use warnings;
my ($line);
my $max = 0;
my @prediction;
foreach my $line (<DATA>) {
chomp($line);
if ($line =~ /^>/) {
print "$line";
}
else {
my @split = split(/\s+/,$line);
my $score = pop @split;
if ($max < $score) {
$max = $score;
@prediction = @split;
push @prediction, $max;
}
#print "$domain\n";
}
print "@prediction\n";
}
__DATA__
>dna1
D 124.6
D T 137.5
G -3.4
G T 9.5
T 12.9
>dna2
A 196.3
A K 186.5
A K H 192.7
A H M 206.2
A M 200
输出
>dna1
D 124.6
D T 137.5
D T 137.5
D T 137.5
D T 137.5
>dna2D T 137.5
A 196.3
A 196.3
A 196.3
A H M 206.2
A H M 206.2
请你帮我弄清楚我如何能得到最终得分与最高得分的输出如下:
>dna1
D T 137.5
>dna2
A H M 206.2
谢谢!
答案 0 :(得分:1)
以下产生预期的输出:
#!/usr/bin/perl
use warnings;
use strict;
my ($max_score, $max_combination);
while (my $line = <DATA>) {
if ($line =~ /^>/) {
print "$max_combination $max_score\n" if $max_combination;
print $line;
$max_score = 0;
} elsif (my ($combination, $score) = $line =~ /(.*)\s+([0-9.]+)/) {
if ($score > $max_score) {
$max_score = $score;
$max_combination = $combination;
}
}
}
print "$max_combination $max_score\n";
主要区别在于您只需在新组开始时或最后时打印结果。