计算给定数组中大于5的数字

时间:2016-10-16 19:26:09

标签: arrays perl

我遇到错误,指出prototype not terminated at filename.txt line number 113第113行属于另一个成功运行的程序。

sub howmany( 
    my @H = @_;
    my $m = 0;

    foreach $x (@H) {
        if ( $x > 5 ) {
            $m +=1;
        }
        else { 
            $m +=0;
        }
    }
    print "Number of elements greater than 5 is equal to: $m \n";
} 
howmany(1,6,9);

2 个答案:

答案 0 :(得分:3)

sub关键字应该跟{ }而不是( )(如果你定义一个简单的函数),这就是错误的原因

prototype not terminated

在此之后,请始终以:use strict; use warnings;

开头

把这个和调试你的脚本,还有更多的错误。

最后但并非最不重要的是,缩进代码,使用带语法突出显示的编辑器,您将节省很多时间调试

答案 1 :(得分:0)

错误是由括号引起的。 永远不要$ m + = 0;因为你实际上没有加载处理器。当然,在这么小的功能上它不可见,但是......

sub howmany {
    my $m = 0;
    foreach (@_) {
        $m++ if ($_ > 5);
    }
    print "Number of elements greater than 5 is equal to: $m \n";
} 
howmany(1,6,9);