比较数组中的值

时间:2015-04-30 16:05:36

标签: arrays perl dbi

我使用perl来处理返回多个结果的数据库查询:

select name,percent from table order by percent desc

我想只检索if条件中的那些值,如下代码所示:

while (@data=$sth->fetchrow_array()) {
    $toto = $data[0];
    $percent = $data[1];
    foreach $percent (@data) {
        if ($percent > 80) {
            $output .= $toto.'='.$percent.'%,';
            $state = "BAD";
        }
        elsif ($percent > 60 && $percent < 80){
            $output .= $toto.'='.$percent.'%,';
            $state = "NOTGOOD";
        }
    }
}
my $str = "$state $output";
# Display Ouptut
print $str."\n";
undef($str);

exit $ERRORS{$status};

此代码仅打印最后一个语句(NOTGOOD);我想为每个缺失的值打印BAD

这是查询的结果:

test 40
test2 80
test3 75
test4 90
test5 50
test6 45

这里是打印输出:

NOTGOOD test4=90%,test2=80%,test3=75%,

所有值都很好但状态错误

0 个答案:

没有答案
相关问题