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?!


In reply to Check/set hash of hashes value by stroke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.