如何解引用数组中的标量引用。

huangapple go评论66阅读模式
英文:

How to deference a reference to a scalar in an array

问题

以下是翻译好的部分:

我有以下的代码段

    $var1 = 10;
    
    @arr = (1, $var1, 3);
    
    print "var1= $$arr[1] \n";

这不会打印出值为10,`$$arr[1]` 这个语法正确吗?
使用额外的变量,我能够打印出这个值

    $r = $var[1];
    
    print "var1 = $$r\n";
英文:

I have following piece of code

$var1 = 10;

@arr = (1, $var1, 3);

print "var1= $$arr[1] \n";

This is not printing value 10, is this syntax $$arr[1] correct?
using additional variable i was able to print the value

$r = $var[1];

print "var1 = $$r\n";

答案1

得分: 5

如果是 $NAME,则表示你有名称;如果是 $BLOCK,则表示你有一个引用。所以,

${ $arr[1] }

或者(5.24+)

$arr[1]->*

或者(5.20+)

use experimental qw( postderef );

$arr[1]->*

参考资料:

英文:

If it's $NAME if you have the name, it's $BLOCK if you have a reference. So,

${ $arr[1] }

or (5.24+)

$arr[1]->$*

or (5.20+)

use experimental qw( postderef );

$arr[1]->$*

References:

答案2

得分: 1

print "var1 = ${ $var[1] }\n";

英文:

Try this

print "var1 = ${ $var[1] }\n" ;

huangapple
  • 本文由 发表于 2020年1月3日 16:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定