gpt4 book ai didi

arrays - 如何绕过 Perl 中 foreach 循环的范围

转载 作者:行者123 更新时间:2023-12-04 02:45:00 25 4
gpt4 key购买 nike

所以我正在处理一些我的编译器真的不喜欢的代码。有两个数组,它们具有相同数量的索引。 @array0 填充,@otherarray 按顺序填充。在此 foreach 循环中,它会跳过第一个值,因为它是在循环外填充的。 Count 在循环外也声明为 1。

foreach (@array) {
if ($count == 1) {
} elsif($_ == 0 && @otherarray[$count-1] != undef) {
$_ = $count;
splice(@otherarray, @otherarray[$count - 1], 1);
} else {
$_ = $otherarray[ rand @otherarray ];
}
$count++
}

它坚持我在这一行的数字 ne(!=) 中使用了未初始化的值,并且其他数组在 else/if/elsif 语句中的每一行:

elsif($_ == 0 && @otherarray[$count-1] != undef) 

我该如何解决这个问题?我敢肯定这很明显,但我真的是 Perl 的新手,所以我可能一开始就设置错了?我已经声明了我的 @otherarray

最佳答案

未初始化的是比较中的undef。使用 defined而不是与 undef 进行比较:

elsif($_ == 0 && defined($otherarray[$count-1]))

关于arrays - 如何绕过 Perl 中 foreach 循环的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19005650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com