Hash of arrays of hashes... There's got to be a cleaner way than this...



Hi,

I've been modifying a perl script that reads a configuration file,
validates the contents and generates a bunch of system configuration
files and an iptables firewall set. I discovered that the change from
ifconfig to iproute2 style files required a change in the way that
aliases were handled so the data no-longer fitted into my hash of arrays.

So I have created a hash keyed by interface containing an array of
hashes keyed by interface values ip, mtu, qdisc... etc. It took a while
but I now have a solution just it's a bit long winded and involves
creating a temporary hash.

Anyone able to point out the error of my ways?

Regards

Al



#! /usr/bin/perl -w

# test to test out a Hash of arrays of hashes
#

my %theInterfaceHash = (
'lo' => [ { ip => '127.0.0.1/8', mtu => '16436' } ],
'eth0' => [ { ip => '192.168.58.23/24', mtu => '1500' }, { ip => '192.168.48.23/24', mtu => '1500' }, { ip => '172.16.16.23/16', mtu => '1492' } ],
'eth1' => [ { ip => '192.168.44.23/24', mtu => '1500' } ]
);

# populating the hash.
my $interface = 'eth1';
my $mtu = '1500';
my $ip = '192.168.99.23/24';

print STDERR "ip=$ip\nmtu=$mtu\n";
my @insert = ["ip", "$ip", "mtu", "$mtu" ];
my %tmpHash = (
'ip' => "$ip",
'mtu' => "$mtu"
);
if (exists $theInterfaceHash{$interface})
{
print STDERR "interface exists already\n";
my $count = $#{$theInterfaceHash{$interface}};
$count++;
$theInterfaceHash{$interface}[$count] = \%tmpHash;
}
else
{
print STDERR "new interface\n";
$theInterfaceHash{$interface} = [ { ip => "$ip", mtu => "$mtu" } ];
}

print "interfaces hash \n";
for my $intfce (sort keys %theInterfaceHash)
{
print "$intfce\n";
for my $i (0 .. $#{ $theInterfaceHash{$intfce} })
{
# print STDERR "$i\n";
print "\t";
print "IP=$theInterfaceHash{$intfce}[$i]{ip} MTU=$theInterfaceHash{$intfce}[$i]{mtu}";
print "\n";
}
}

# printing the hash.
#print "interfaces hash\n";
#for my $intfce (keys %theInterfaceHash)
# {
# print "$intfce\n";#
# for my $i (0 .. $#{ $theInterfaceHash{$intfce} })
# {
# print "$i\n";
# my $value;
# my $info;
# while ( ($info, $value) = each %{$theInterfaceHash{$intfce}[$i]} )
# {
# print "\t$info=$value\n";
# }
# }
# }


Attachment: test.pl
Description: Perl program



Relevant Pages

  • Re: object system...
    ... In a typed system you can separate implementation from the interface ... C/I is also unable to effectively facilitate some non-C/I languages, ... Performance of hash tables is less predictable, ...
    (comp.object)
  • Re: object system...
    ... In a typed system you can separate implementation from the interface ... How to express in the language a wish to delegate, A of X to B of Y. ... Performance of hash tables is less predictable, ...
    (comp.object)
  • Re: ISO recommendations for hash table lib
    ... void hdestroy; ... Unfortunately, this interface is unnecessarily rigid, since keys are ... rolling your own standard hash table, since it lacks the generality you ...
    (comp.lang.c)
  • Re: Semantic difference of source files
    ... All public members belong to the interface.) ... I suppose you could save a separate hash for the interface ... If people didn't care about compilation speed, why do they use the PIMPL ...
    (comp.compilers)
  • Re: pitfalls in code below? or btr ways?
    ... > One way I can think of is have a hash of arrays declared before.. ... you have a hash of hashes. ... perldoc -f exists ... The elements of these arrays are references to arrays that contain the ...
    (perl.beginners)