添加if语句的计数器

时间:2014-04-27 17:05:02

标签: perl counter

如何在此声明中添加计数器。

# 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++),因为它会提取哈希值中的所有值。

1 个答案:

答案 0 :(得分:1)

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: $count\n";