加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl中的postfix for循环不能正常工作

发布时间:2020-12-15 21:48:01 所属栏目:大数据 来源:网络整理
导读:以下行完美地运作 for(my $i=0; $i ($max_size - $curr_size) ; $i++){ push (@{$_},0);} 但是这个没有. push (@{$_},0) for (1 .. ($max_size - $curr_size)); 它给我一个这样的错误信息: Can't use string ("1") as an ARRAY ref while "strict refs" in
以下行完美地运作
for(my $i=0; $i < ($max_size - $curr_size) ; $i++){
    push (@{$_},0);
}

但是这个没有.

push (@{$_},0) for (1 .. ($max_size - $curr_size));

它给我一个这样的错误信息:

Can't use string ("1") as an ARRAY ref while "strict refs" in use at coordReadEasy.pl line 124,<DATA> line 16.

为什么?我该怎么解决这个问题?

解决方法

for的范围版本为每个元素设置$_,所以在@ {$_}中你试图取消引用$_,好像它是一个数组引用.这些都是等价的:
for my $x (1..10) {
  print "$xn"
}

for (1..10) {
  print "$_n"
}

print "$_n" for (1..10);

简单的解决方案是为数组引用创建另一个变量:

push @{$ref},0 for 1 .. $max_size - $curr_size;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读