将计数器添加到if语句



如何向该语句添加计数器。

# go through each reference file
for my $file (@reference_files) 
{
    open my $ref, "<", $file or die "Can't open reference file '$file': $!";
    while (my $line = <$ref>) 
    {
        chomp $line;
        my ($scaffold, undef, $type, $org_snp, $new_snp, undef, undef, undef, $info) = split /t/, $line;
        next if not $scaffold =~ /^KB/;
        next if not $type =~ /^GENE/i;
        my ($transcript_id, $gene_name, $auto) = split /[;][ ]/, $info;
        $gene_name = $1 if $gene_name =~ /["]([^"]*)["]/;
        if (my $matching_genes = $genes{$scaffold}) 
        {
            say join "t", $gene_name, $_ for values %$matching_genes;
        }
    }
    say "###";
}

我希望脚本能够额外计算所有$matching_genes。有没有办法把这一点结合起来?我使用标准计数器(即$i++)失败了,因为它正在提取哈希中的所有值。

for循环之前,可以在顶部初始化一个全局计数器变量为0,比如:

my $counter = 0;
# go through each reference file
for my $file (@reference_files)
# ... Rest of your code ...

然后,您可以在分配$matching_genesif语句中递增$counter

if (my $matching_genes = $genes{$scaffold}) 
{
        $counter++;
        say join "t", $gene_name, $_ for values %$matching_genes;
}
my $count=0;
# go through each reference file
for my $file (@reference_files) 
{
    open my $ref, "<", $file or die "Can't open reference file '$file': $!";
    while (my $line = <$ref>) 
    {
        chomp $line;
        my ($scaffold, undef, $type, $org_snp, $new_snp, undef, undef, undef, $info) = split /t/, $line;
        next if not $scaffold =~ /^KB/;
        next if not $type =~ /^GENE/i;
        my ($transcript_id, $gene_name, $auto) = split /[;][ ]/, $info;
        $gene_name = $1 if $gene_name =~ /["]([^"]*)["]/;
        if (my $matching_genes = $genes{$scaffold}) 
        {
            say join "t", $gene_name, $_ for values %$matching_genes;
            $count =+ scalar(keys %$matching_genes);
        }
    }
    say "###";
}
print "total: $countn";

相关内容

  • 没有找到相关文章

最新更新