in reply to Re^3: variable inside hash key
in thread variable inside hash key

The problem with this is, i need to use the output &$key_MS, with in a hash key and it may not work

my $key_MS = sub { "R3R3_${pic}_IF"; }; print "set service-interface $v->{&$key_MS}.0\n";

Replies are listed 'Best First'.
Re^5: variable inside hash key
by Anonymous Monk on Apr 06, 2016 at 12:23 UTC

    Works for me. But when in doubt, use the . concat operator, like in the second example below, or in the case of print, the comma operator like in the third example. Otherwise, please be more specific than "it may not work", and provide a piece of example code where it fails (see How do I post a question effectively?).

    my $v = { R3R3_1_IF=>"xx", R3R3_2_IF=>"yy", R3R3_3_IF=>"zz" }; my $pic = 1; my $key_MS = sub { "R3R3_${pic}_IF"; }; print "set service-interface $v->{&$key_MS}.0\n"; $pic++; print "set service-interface " . $v->{&$key_MS} . ".0\n"; $pic++; print "set service-interface ", $v->{&$key_MS}, ".0\n"; __END__ set service-interface xx.0 set service-interface yy.0 set service-interface zz.0

      Thank you monks. My bad! I specified wrong variable during increment !