for循环在Perl中 [英] for loop in perl

查看:157
本文介绍了for循环在Perl中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my @array=(1..10);
for my $i (@array){$i++;}
print "array is now:@array";

这正在改变数组的值。为什么?

this is changing the values of the array. Why?

推荐答案

这就是Perl中语句的去做。请参阅 man perlsyn Foreach Loops 的文档。 :

This is what the for statement in Perl is defined to do. See the documentation for Foreach Loops in man perlsyn:


如果LIST的任何元素是一个左值,您可以通过修改循环内的VAR来修改它。相反,如果LIST的任何元素不是左值,则任何修改该元素的尝试都将失败。换句话说,foreach循环索引变量是列表中每个项目的隐式别名。

If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

这篇关于for循环在Perl中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆