[英]perl quits loop early
my program has 2 variables in the target_services array 我的程序在target_services数组中有2个变量
DB<1> x @target_services
0 3400000000000012
1 3400000000000011
this is the code it hits 这是它命中的代码
foreach my $i (@target_services){
my $vl = shift @values || "";
my $dp = shift @descriptions || "";
my $ts_id = shift @target_services;
my $lp = shift @lp_values;
if (get_lp($ts_id,$lp) eq 'YES'){
print "ts id $ts_id already has $lp LP. Aborting addition of this LP for this TS\n";
next;
}
my $temp_query = $sql3;
$temp_query =~ s/TS/$ts_id/;
$temp_query =~ s/LP/$lp/;
$temp_query =~ s/VL/$vl/;
$temp_query =~ s/DP/$dp/;
my $sth3 = get_sth($temp_query);
$count_lps+=$sth3->get_count();
$count_ts++;
}
the debugger implies that it goes through the loop once, gets to the next; 调试器暗示它会循环一次,到达下一个循环; and then jumps to my print statement.
然后跳转到我的打印语句。 and never goes through the loop a second time.
永远不会再经历循环
please explain why this is 请解释为什么
It's documented in perlsyn : 它记录在perlsyn中 :
If any part of LIST is an array,
foreach
will get very confused if you add or remove elements within the loop body, for example withsplice
.如果LIST的任何部分是数组,则在循环体内添加或删除元素(例如,使用
splice
,foreach
会非常困惑。 So don't do that.所以不要那样做。
That's exactly what you did: 那就是你所做的:
foreach my $i (@target_services){
# ...
my $ts_id = shift @target_services;
Instead use 改为使用
foreach my $ts_id (@target_services){
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.