krishna reddy
krishna reddy

Reputation: 53

How to deference a reference to a scalar in an array

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";

Upvotes: 1

Views: 79

Answers (2)

ikegami
ikegami

Reputation: 385897

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:

Upvotes: 5

pmqs
pmqs

Reputation: 3705

Try this

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

Upvotes: 1

Related Questions