英文:
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" ;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论