Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: variable inside hash key

by toolic (Bishop)
on Mar 29, 2016 at 14:21 UTC ( [id://1159018]=note: print w/replies, xml ) Need Help??


in reply to variable inside hash key

One way...
use warnings; use strict; my $pic = 1; my $v = {R3R3_1_IF => 555}; my $key = "R3R3_${pic}_IF"; print "set groups interfaces $v->{$key}\n"; __END__ set groups interfaces 555

Replies are listed 'Best First'.
Re^2: variable inside hash key
by Anonymous Monk on Mar 30, 2016 at 01:12 UTC

    Thank you, toolic. It worked

Re^2: variable inside hash key
by Anonymous Monk on Apr 06, 2016 at 05:55 UTC

    I think increment of $pic is not happening with this method

    my $pic = 1; my $key_MS = "R3R3_${pic}_IF"; my $temp = 90; for (my $ip = 1; $ip<=100; $ip++) { if ($temp == $ip) { $pic++; } } print "New pic value is $key_MS\n"; output:New pic value is R3R3_1_IF

      $pic is being incremented. This however has absolutely no effect on $key_MS which was set and evaluated when $pic was still 1. If you want to have it re-evaluated every time it is referenced you would need a closure or an eval or similar. eg. (with the loop replaced for clarity)

      #!/usr/bin/env perl use strict; use warnings; my $pic = 1; my $key_MS = sub { "R3R3_${pic}_IF"; }; print "Old pic value is " . &$key_MS . "\n"; $pic = 2; print "New pic value is " . &$key_MS . "\n";

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found