Hash of arrays of hashes... There's got to be a cleaner way than this...
- From: tetchy <alasdair@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 28 Feb 2007 16:35:08 +0000
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
- Follow-Ups:
- Re: Hash of arrays of hashes... There's got to be a cleaner way than this...
- From: Mark Clements
- Re: Hash of arrays of hashes... There's got to be a cleaner way than this...
- Prev by Date: Re: Howto:Perl connection to Oracle
- Next by Date: Re: CGI:Session & MS SQLServer
- Previous by thread: perl & heredoc to create xml
- Next by thread: Re: Hash of arrays of hashes... There's got to be a cleaner way than this...
- Index(es):
Relevant Pages
|