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

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.

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

        The following code, taken from here, worked for me!

        # Either build your data as a hashref my $hashref = { username => someusername, etc => ..., }; # or assign a reference to a hash my %data = (username => someusername, etc => ...); my $hashref = \%data; # Then pass it off my $valid = verify($hashref); # You can also pass like so my $valid = verify(\%data); # Then, in your verify sub: sub verify { my $ref = shift; # do something with your data $ref->{'username'} =~ /yourregex/; return $something; }