1

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";
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    Need `print "var1= ${$arr[1]}\n";` I suggest to read over [perlreftut](https://perldoc.perl.org/perlreftut.html) – zdim Jan 03 '20 at 09:03
  • 1
    `$$arr[1]`, short for `${ $arr }[1]`, and equivalent to `$arr->[1]`, is for when you have a reference to an array, and you want the second element of the referenced array. You have a reference to a scalar. – ikegami Jan 03 '20 at 09:17

2 Answers2

5

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:

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

Try this

print "var1 = ${ $var[1] }\n" ;
pmqs
  • 3,066
  • 2
  • 13
  • 22