perl foreach循环数组,简单的问题
非常简单的 Perl 问题,但让我很困惑。
foreach $val (@{$obj->something()}) { # this works } @array = $obj->something(); foreach $val (@array) { # this does not }
我需要做什么才能完成第二项工作(即:单独分配数组),我已经使用了相当多的第一种形式,但并不真正理解它的不同之处。
Really simple perl question, but confusing me greatly.
foreach $val (@{$obj->something()}) { # this works } @array = $obj->something(); foreach $val (@array) { # this does not }
What do i need to do to make the second work (i.e: assign the array seperately), i've used the first form a fair bit but dont really understand what it does differently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能:
从第一个示例中,看起来
$obj->something()
返回一个数组引用,您需要取消引用它。另外,您确实应该
use strict;
和use warnings;
,并像这样声明您的变量,这将使您更容易发现错误(尽管可能不是这个),即使在三行脚本中。
Probably:
From the first example, it looks like
$obj->something()
returns an array reference, you'll need to dereference it.Also, you should really
use strict;
anduse warnings;
, and declare your variables likeThis will make it much easier to find mistakes (although probably not this one), even in a three line script.