Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Passing a variable to a hash

by sumeetgrover (Monk)
on Jan 03, 2012 at 14:10 UTC ( [id://946066]=note: print w/replies, xml ) Need Help??


in reply to Passing a variable to a hash

Hi,

The problem in your code is that you have assigned $vl1, $vl2 and $vl3 as values to hash keys:

my %vlan = ( "VLAN1" => $vl1, "VLAN2" => $vl2, "VLAN3" => $vl3, )<br>

BUT the values of these variables are 'assigned' much later through user input. At the time of 'initialisation' of hash, these values are null; The user input is taken much later, by which time, the hash values are already set to null.

Hopefully someone else will post a full solution for you here, but due to time constraints I cannot. Hope this information is helpful to begin with!

Replies are listed 'Best First'.
Re^2: Passing a variable to a hash
by Anonymous Monk on Jan 03, 2012 at 21:03 UTC

    Just wanted to point out that the OP can achieve this by using references (which are probably above his head right now and far from ideal for this exact problem, but just trying to be complete):

    my %vlan = ( "VLAN1" => \$vl1 ); # ... while (($key, $value) = each %vlan) { # note the dereferencing ${$var} # can be shortened to $$value print "Now checking: ${system}_$key ${$value}"; }

    Oh, and it is preferred to delimit variables with "${var}" when interpolating. (This is not related to dereferencing.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-23 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found