微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Perl的foreach和更改循环变量

我在Perl中写一个脚本,并且有关于Perl的foreach结构的问题.

看来,如果您更改其中一个循环变量,它将在实际数组中更改.这实际上是这样,还是我做了完全错误的事情?

我想改变一个字符串,如abc.abc#a到abc_abc_a(非字母数字字符的下划线),但是我需要保留数组中的原始值以备以后使用.

我的代码看起来像这样:

@strings = ('abc.abc#a','def.g.h#i');
foreach my $str (@strings){
    $str =~ s/[^0-9A-Za-z]/_/g;
    print $str,"\n"; #Actually I use the string to manipulate files.
}

我可以通过执行以下操作来解决问题:

@strings = ('abc.abc#a','def.g.h#i');
foreach my $str (@strings){
    my $temp = $str; #copy to a temporary value
    $temp =~ s/[^0-9A-Za-z]/_/g;
    print $temp,"\n"; #$str remains untouched...
}

但是有没有更有效的方式来完成这个?

非常感谢你!

解决方法

你不是疯了这是正常的行为.请参阅 perldoc perlsyn在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.

其他循环迭代器,如map具有类似的行为:

06000


Note that $_ is an alias to the list value,so it can be used to modify the
elements of the LIST. While this is useful and supported,it can
cause bizarre results if the elements of LIST are not
variables. Using a regular “foreach” loop for this purpose would be
clearer in most cases.
See also “grep” for an array composed of those items
of the original list for which the BLOCK or EXPR evaluates to true.

您可以通过这种方式重写代码,这至少可以节省您添加额外的行:

my @strings = ('abc.abc#a','def.g.h#i');
foreach my $str (@strings){
    (my $copy = $str) =~ s/[^0-9A-Za-z]/_/g;
    print $copy,"\n";
}

原文地址:https://www.jb51.cc/Perl/172722.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐