我有一个像@ array =(1,2,3,4)
这样的数组我也有一个标量,让我们说2.我如何检查并看到有一个小于2的数字,一个数字等于2,2个数字大于2?
答案 0 :(得分:0)
您可以遍历每个元素并添加到计数器,如下所示:
#!/usr/bin/perl
use strict;
use warnings;
my @array = qw/ 1 2 3 4/;
my $lower = 0;
my $equal = 0;
my $higher = 0;
for my $i (@array)
{
if ($i < 2) { $lower += 1; }
elsif ($i == 2) { $equal += 1; }
else { $higher += 1; }
}
print "There are $lower elements lower than 2\n";
print "There are $equal elements equal to 2\n";
print "There are $higher elements higher than 2\n";