【发布时间】:2015-07-10 10:39:40
【问题描述】:
我正在使用 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%,
所有值都很好但状态错误
【问题讨论】:
-
始终使用严格。你在哪里打印 $output 和 $state?
-
$opt_c 和 $opt_w 是数字,而 opt_w 小于 opt_c,我认为问题出在循环外打印。我要改了
-
我在最后打印 $output 和 $state
-
好的。那么给定这个输入,你到底想要什么输出?