gpt4 book ai didi

perl - 范围运算符从最大到最小递减 : 10. .1

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

这个问题在这里已经有了答案:





Alternate for making such a thing work in perl : `for(10...0)`

(3 个回答)


6年前关闭。




Perl 有一个范围操作符,当在 foreach 中使用时循环,不创建临时数组:

foreach (1 .. 1_000_000) {
# code
}

如果第一个整数小于第二个整数,则不运行迭代:
foreach (1_000_000 .. 1) {
# code here never runs
}

我可以使用 reverse内置,但这会保持不创建临时数组的优化吗?
foreach (reverse 1 .. 1_000_000) {
# code
}

有没有一种方法可以像范围运算符一样漂亮和快速地减少数字而不是增加数字?

最佳答案

不漂亮的解决方案,

for (my $i=1_000_000; $i >= 1; $i--) {

print "$i\n";
}

关于perl - 范围运算符从最大到最小递减 : 10. .1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505835/

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