Perl 数组问题

wxgiter 2010-12-23 12:51:15
use warnings;
use strict;

my @list = ("123","124","125");

foreach(@list)
{
print $_,"\n";
}

my $temp;
foreach $temp(@list)
{
$temp =~ s/2/a/g;#HERE!!
}

foreach(@list)
{
print $_,"\n";
}

my $i= 0;
my $var;
for($i=0; $i<scalar(@list); $i++)
{
$var = $list[$i];
$var =~ s/1/r/g;#HERE!!
}

foreach(@list)
{
print $_,"\n";
}


如代码所示,第一次替换后,怎么会改变了数组的内容?
按第二种方法就没有改变数组。
...全文
106 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxgiter 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fibbery 的回复:]

引用 7 楼 wxgiter 的回复:
引用 5 楼 fibbery 的回复:

拘小节。

是说钻牛角尖?


不是,呵呵,我的意思是说,写代码要拘小节。仁者见仁,智者见智吧,不带任何感情色彩。呵呵。
[/Quote]
等于是又把 拘小节 说了一遍。--!
fibbery 2010-12-23
  • 打赏
  • 举报
回复
没有褒贬,只是建议。呵呵
fibbery 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxgiter 的回复:]
引用 5 楼 fibbery 的回复:

拘小节。

是说钻牛角尖?
[/Quote]

不是,呵呵,我的意思是说,写代码要拘小节。仁者见仁,智者见智吧,不带任何感情色彩。呵呵。
wxgiter 2010-12-23
  • 打赏
  • 举报
回复
明白了,谢谢大家回复。
等7楼回复就结贴,哈哈
wxgiter 2010-12-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fibbery 的回复:]

拘小节。
[/Quote]
是说钻牛角尖?
fibbery 2010-12-23
  • 打赏
  • 举报
回复
Foreach Loops
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localisation occurs only in a foreach loop.

The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. (Or because the Bourne shell is more familiar to you than csh, so writing for comes more naturally.) If VAR is omitted, $_ is set to each value.

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.

If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.

foreach probably won't do what you expect if VAR is a tied or other special variable. Don't do that either.

fibbery 2010-12-23
  • 打赏
  • 举报
回复
拘小节。
Aylazhang 2010-12-23
  • 打赏
  • 举报
回复
第二种方法,已经把数组元素copy到$var里了。当然不会修改原来的值。
第一种方法, foreach xxx ;这里的xxx应该是数组每个元素的一个ref
看着奢扣 2010-12-23
  • 打赏
  • 举报
回复
foreach循环的控制变量($temp)不是列表元素的备份,它本身就逐次代表列表的元素本身,循环时如果修改了变量,原始的列表元素就会被修改。
Haven 2010-12-23
  • 打赏
  • 举报
回复
另外,我的习惯是,不论会不会改变这个值。我是不会直接对这个值进行修改。如果想改,我直接去改数组元素,如果不想改,就用变量去传递值。这样既容易读,也不易错。
Haven 2010-12-23
  • 打赏
  • 举报
回复
在一个foreach循环中,迭代器并不只是设置为数据(或称为列表)中的每个元素的值,它实际上是对数组的元素的引用。因此,在上面这个foreach循环中,如果修改该循环中的$temp,就能修改@list中的对应元素。
而后面的例子,你用$var中转了数据元素的值,显然就不再是对数组进行引用,而只是值的传递。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧