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.)