in reply to Check/set hash of hashes value

Thanks people - that did the trick ! I've tested out both and they work. But perhaps for me, this is more obvious and will make it easier for me to work out exactly what I'm doing here, should I need to look back on it:

foreach my $ip ( keys %subnets ) { $subnets{$ip}{comment} //= 'ALLOCATED'; }

Thanks again

Replies are listed 'Best First'.
Re^2: Check/set hash of hashes value
by sammy_01 (Initiate) on Jun 25, 2013 at 05:43 UTC

    You can use this way as well..

    foreach $ip (keys(%subnets)) { if("$subnets{$ip}{'comment'}" eq "") { $subnets{$ip}{'comment'} = "ALLOCATED"; } print "$subnets{$ip}{'comment'}\n"; }
      If you are testing if a value is defined, you should test that. It is possible (not knowing this person's dataset) that empty string is a legitimate value. exists, defined and eq are different for a reason.

      If you use warnings, Perl will emit a warning every time you compare with an undefined value; it's a helpful tool to figure out if you've forgotten to initialize a variable. With your code and warnings, there would be a large amount of unnecessary noise on STDERR.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.