Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Creating Multidimensional Hashed Arrays?

by spickles (Scribe)
on Aug 27, 2009 at 22:40 UTC ( [id://791764]=perlquestion: print w/replies, xml ) Need Help??

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

PerlMonks -

I'm trying to create an array of switch VLANs (by number) with additional characteristics such as a name, description, IP address, etc. I would like the VLANs identified by their number so I can sort and then print. My code below only creates a file containing the text I am specifying in the quotes, and none of the variables. STDERR complains that I'm using uninitialized variables.

%myarray = (vlans => {[ {number => "112", name => "JP-WIRELESS", description => "JP-WIRELESS", ip => "192.168.207.1", mask=> "255.255.255.0" }, {number => "2113", name => "JP-IDF1-DATA", description => "JP-IDF1-DATA", ip => "10.136.113.1", mask=> "255.255.255.0" } ]} ); sub print_array { unlink "vlandatabase.txt"; open (MYFILE,'>',"vlandatabase.txt"); print MYFILE %myarray; print MYFILE "VLAN number:$myarray{vlans}[0]{number}\n"; print MYFILE "VLAN name:$myarray{vlans}[0]{name}\n"; print MYFILE "VLAN description:$myarray{vlans}[0]{description}\n" +; print MYFILE "VLAN ip address:$myarray{vlans}[0]{ip}\n"; print MYFILE "VLAN subnet mask:$myarray{vlans}[0]{mask}\n"; close MYFILE; }

Once I can understand how to do this, how do I loop through and print?

Replies are listed 'Best First'.
Re: Creating Multidimensional Hashed Arrays?
by GrandFather (Saint) on Aug 27, 2009 at 23:51 UTC

    Running your code through PerlTidy may help:

    my %myarray = ( vlans => { [ { number => "112", name => "JP-WIRELESS", description => "JP-WIRELESS", ip => "192.168.207.1", mask => "255.255.255.0" }, { number => "2113", name => "JP-IDF1-DATA", description => "JP-IDF1-DATA", ip => "10.136.113.1", mask => "255.255.255.0" } ] } );

    Notice that PerlTidy is confused about the nested hash (it has aligned the {} for it with the [] for the nested array). That's a strong clue, especially when combined with the 'Odd number of elements in anonymous hash ...' warning you get when using strictures, that something isn't cosher with the nested hash.

    Maybe you don't want the nested hash at all? Or maybe you are missing the keys for it and don't want the nested array?


    True laziness is hard work
      The most important detail here is that I don't know HOW to do what I want. As I mentioned in my original post, I need to track information regarding some VLANs that will be entered via subroutines that ask for input. It's difficult to explain, but it's a setup script for a Cisco switch. One subroutine prompts for information about layer 2 VLANs (number and name), while the other asks for layer 3 VLANs (number, description, IP address, subnet mask). To make efficient use of this data, I want to structure it similar to a table, where the VLAN number is the primary key, and all other data is a 'column' of that 'row' (each VLAN). I then want to be able to sort by VLAN number prior to making use of it. Ultimately I need to understand how to structure the data, then how to add items and retrieve them.

      Regards,
      Scott

        Draw your data out on a piece of paper with a box around each data item (nested as appropriate). Where sensible give boxes names.

        Each named box is a key/value pair and should be in a hash. Each unnamed box is an array element. The nesting of the boxes tells you how the hash and array elements nest.

        Keep your piece of paper around to refresh you understanding of how the data hangs together as you write the code. At each point that you need to access something you need to work from the outermost box to the box of interest taking note of whether each box is an array element or a hash value.


        True laziness is hard work
        Maybe you want to structure your data as a HASHES OF HASHES, with the keys being the VLAN number (if they are unique), then you can sort by keys and print the data:
        use warnings; use strict; my %vlan_nums = ( 112 => { name => "JP-WIRELESS", description => "JP-WIRELESS", ip => "192.168.207.1", mask => "255.255.255.0" }, 2113 => { name => "JP-IDF1-DATA", description => "JP-IDF1-DATA", ip => "10.136.113.1", mask => "255.255.255.0" } ); for my $num (sort keys %vlan_nums) { print <<EOF; VLAN number : $num VLAN name : $vlan_nums{$num}{name} VLAN description: $vlan_nums{$num}{description} VLAN ip : $vlan_nums{$num}{ip} VLAN mask : $vlan_nums{$num}{mask} EOF }

        Which prints out:

        VLAN number : 112 VLAN name : JP-WIRELESS VLAN description: JP-WIRELESS VLAN ip : 192.168.207.1 VLAN mask : 255.255.255.0 VLAN number : 2113 VLAN name : JP-IDF1-DATA VLAN description: JP-IDF1-DATA VLAN ip : 10.136.113.1 VLAN mask : 255.255.255.0

        Data::Dumper is also handy for displaying Perl data structures.

Re: Creating Multidimensional Hashed Arrays?
by toolic (Bishop) on Aug 27, 2009 at 23:40 UTC
    use strict and warnings and diagnostics:
    C:\> perl -w -Mdiagnostics 791764.pl Odd number of elements in anonymous hash at 791764.pl Line 1 (W misc) You specified an odd number of elements to initialize a has +h, which is odd, because hashes come in key/value pairs.

Log In?
Username:
Password:

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

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

    No recent polls found