Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Creating Multidimensional Hashed Arrays?

by GrandFather (Saint)
on Aug 27, 2009 at 23:51 UTC ( [id://791776]=note: print w/replies, xml ) Need Help??


in reply to Creating Multidimensional Hashed Arrays?

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

Replies are listed 'Best First'.
Re^2: Creating Multidimensional Hashed Arrays?
by spickles (Scribe) on Aug 28, 2009 at 01:51 UTC
    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.

        toolic -

        You nailed it. I was reading the perl doc you linked for a couple of hours before posting, so I was on the right track. But decoding all of those parenthesis, brackets, commas, etc. can get confusing quickly. So now my question would be how to add a new vlan?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2025-07-13 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.