rspishock has asked for the wisdom of the Perl Monks concerning the following question:

Good morning all.

This is the first time that I've actually worked with hashes and I'm stuck on something that's probably going to be a very easy fix. I'm trying to automate a system tool that scans multiple VLANs, however, I'm having trouble passing the variable to the hash.

Below is the section of code in which I the problem is occuring. Everything works as expected, except, the hash value is not being printed out to the screen.

use strict use warnings use diagnostics my ($key, $system, $value, $vl1, $vl2, $vl3); my %vlan = ( "VLAN1" => $vl1, "VLAN2" => $vl2, "VLAN3" => $vl3, ) -- LINES OMITTED -- print "VLAN1: "; chomp ($vl1 = <STDIN>); print "VLAN2: "; chomp ($vl2 = <STDIN>); print "VLAN3: "; chomp ($vl3 = <STDIN>); -- LINES OMITTED -- while (($key, $value) = each %vlan) { print "Now checking: $system\_$key $value"; -- LINES OMITTED -- }

When I run this code, I get for example:

systemName_VLAN1
However, I am hoping to get an output of:
systemName_VLAN1 1.1.1.1
providing that the user entered a value of 1.1.1.1 into $vl1.

Any help as to what I'm missing or maybe a better way to do this would be greatly appreciated.

Thanks in advance for your infinite wisdom!

Replies are listed 'Best First'.
Re: Passing a variable to a hash
by davido (Cardinal) on Jan 03, 2012 at 14:09 UTC

    You don't need $v1, $v2, etc. And in my opinion whenever you start naming variables (or hash keys) with numbers you probably should be using an array anyway. But sticking with the hash, this would probably do what you're attempting.

    use strict; use warnings; my $system = 'something'; my %vlan; # --- Lines Omitted --- my $vl_number = 1; while( <STDIN> ) { chomp; $vlan{ 'VLAN' . $vl_number++ } = $_; } # --- Lines Omitted --- while( my ( $key, $value ) = each %vlan ) { print "Now checking: $system\_$key $value\n"; }

    Dave

      Dave </P

      Thanks for your help.  $vl1, $vl2... were just examples, I had to change the variable names when I posted them here. However, your reply has me thinking ahead as to how I might change this script to accommodate a scan when all of the VLANs are not present.

      Thanks again for the guidance.

Re: Passing a variable to a hash
by Ratazong (Monsignor) on Jan 03, 2012 at 14:04 UTC

    You should move the block starting with my %vlan after the lines that populate $vl1, $vl2... from <STDIN>. Then your code should be fine (I didn't test it however).

    HTH, Rata

      Thanks for the help, that worked. I knew it had to be something simple.

Re: Passing a variable to a hash
by sumeetgrover (Monk) on Jan 03, 2012 at 14:10 UTC

    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!

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

Re: Passing a variable to a hash
by cavac (Prior) on Jan 03, 2012 at 15:20 UTC

    <sidenote type="informational">

    Good morning all.

    At the location on earth from which i read your post, it was 14:53 CET when you wrote it. So i think a "good afternoon" is more likely in order.

    ;-)

    </sidenote>

    Update: Here's a little statistic of population per time zone, in case you wonder what the world's most widely used time zones actually are.

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot