Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Creating Multidimensional Hashed Arrays?

by spickles (Scribe)
on Aug 28, 2009 at 01:51 UTC ( [id://791801]=note: print w/replies, xml ) Need Help??


in reply to Re: Creating Multidimensional Hashed Arrays?
in thread Creating Multidimensional Hashed Arrays?

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
  • Comment on Re^2: Creating Multidimensional Hashed Arrays?

Replies are listed 'Best First'.
Re^3: Creating Multidimensional Hashed Arrays?
by GrandFather (Saint) on Aug 28, 2009 at 02:02 UTC

    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
Re^3: Creating Multidimensional Hashed Arrays?
by toolic (Bishop) on Aug 28, 2009 at 02:23 UTC
    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?

        to add a new vlan assuming vlan number is unique
        unless (exists $vlan_nums{$new_num}){ $vlan_nums{$new_num}{name} = xyz; .... }
        If update is needed for the existing number you can add an else loop

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found