且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Perl 中的 foreach 循环中自动获取循环索引

更新时间:2022-05-08 21:41:19

Like codehead 说,你必须迭代数组索引而不是它的元素.与 C 风格的 for 循环相比,我更喜欢这个变体:

Like codehead said, you'd have to iterate over the array indices instead of its elements. I prefer this variant over the C-style for loop:

for my $i (0 .. $#x) {
    print "$i: $x[$i]
";
}