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

Re: variable inside hash key

by AnomalousMonk (Archbishop)
on Mar 29, 2016 at 19:42 UTC ( [id://1159045]=note: print w/replies, xml ) Need Help??


in reply to variable inside hash key

I think I prefer toolic's computation of  $key as a separate step (more readable/maintainable), but if you really need it all in one statement:

c:\@Work\Perl>perl -wMstrict -le "my $v = { R3R3_foo_IF => 'xyzzy', R3R3_bar_IF => 'wibble', }; ;; my $pic = 'foo'; ;; print qq{whatever $v->{ qq{R3R3_${pic}_IF} } and so on}; " whatever xyzzy and so on
(Note that I use  qq{...} instead of  "..." only to avoid distracting  \ escapes on the Windows command line.)

Update: The trick is to realize that  $v->{ qq{R3R3_${pic}_IF} } is really  $v->{ EXPR } where EXPR is any expression whatsoever (but not a statement). So  $v->{ qq{R3R3_${pic}_IF} } evaluates to a scalar which is double-quote-ishly interpolated as usual.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-29 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found