在使用 for
在 Perl 中循环符号时,我遇到了相当大的困难,找出一些奇怪的行为环形。
此代码片段按预期工作:
for (my $file = 'a'; $file le 'h'; $file++) {
print $file;
}
输出: abcdefgh
但是当我尝试向后循环符号时,如下所示:
for (my $file = 'h'; $file ge 'a'; $file--) {
print $file;
}
结果给了我以下内容。
输出:小时
也许当涉及符号时,递减运算符的行为不像我认为的那样?
有人对此事有任何想法吗?我真的很感激你的帮助!
问候,
汤米
最佳答案
根据 perlop,自动递减运算符并不神奇
你可以做这样的事情:
for my $file (reverse 'a' .. 'h') {
print $file;
}
关于perl - 在 Perl 中循环字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9086289/