stroke has asked for the wisdom of the Perl Monks concerning the following question:
I've a script which produces the following output:
subnet,prefix,name 10.0.0.0,30,Site ABC 10.0.0.4,30,ISDN dial-up for Ex House 10.0.0.8,30, 10.0.0.12,30,P2P link
Before printing the output, I would like to check if the "name" field is undef and if so, set a value. The data is in a hash, and when I dump out the values, I think this is a hash of hashes:
$VAR1 = { '10.0.0.0,30' => { 'comment' => 'Site ABC', 'ipn' => 167772160, 'maskn' => 4294967292 }, '10.0.0.4,30' => { 'comment' => 'ISDN dial-up for Ex House', 'ipn' => 167772164, 'maskn' => 4294967292 }, '10.0.0.8,30' => { 'comment' => undef, 'ipn' => 167772168, 'maskn' => 4294967292 }, '10.0.0.12,30' => { 'comment' => 'P2P link', 'ipn' => 167772172, 'maskn' => 4294967292 },
So, I think i need to iterate over the outer hash and then check if the value for 'comment' in the inner hash is undef; if it is, then set it. I have struggled to work out how to use the comment key in the inner hash, I came up with the following, but it doesn't work (the data is in %subnets):
foreach my $ip ( keys %subnets ) { if (undef $subnets{$ip}{$comment}) { $subnets{$ip}{$comment} = 'ALLOCATED'; } }
Where did I go wrong?!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Check/set hash of hashes value
by davido (Cardinal) on Jun 24, 2013 at 16:09 UTC | |
by stroke (Acolyte) on Jun 24, 2013 at 17:30 UTC | |
by davido (Cardinal) on Jun 24, 2013 at 17:43 UTC | |
by kennethk (Abbot) on Jun 24, 2013 at 17:31 UTC | |
|
Re: Check/set hash of hashes value
by hdb (Monsignor) on Jun 24, 2013 at 16:18 UTC | |
|
Re: Check/set hash of hashes value
by kennethk (Abbot) on Jun 24, 2013 at 16:29 UTC | |
|
Re: Check/set hash of hashes value
by stroke (Acolyte) on Jun 24, 2013 at 17:41 UTC | |
by sammy_01 (Initiate) on Jun 25, 2013 at 05:43 UTC | |
by kennethk (Abbot) on Jun 25, 2013 at 15:47 UTC |