Setting default values ​​for undefined hash values

I have a hash whose values ​​are mostly integer and some are undefined. I would like to set all these undefined values ​​to zero, either using a for loop, or, preferably, with a more elegant method. Can anyone suggest a solution?

+6
source share
1 answer

Both methods use foreach,

 for my $key (keys %hash) { $hash{$key} //= 0; } $_ //= 0 for values %hash; 

//= test statement if the variable is undefined and assigns a new value when it is.

+11
source

Source: https://habr.com/ru/post/948739/


All Articles