I have a hash whose values are mostly integers, but some are undefined. I would like to set all of these undefined values to zero, either with a for
loop or, preferably, with a more elegant method. Can someone suggest a solution?
Both methods are using foreach,
for my $key (keys %hash) {
$hash{$key} //= 0;
}
$_ //= 0 for values %hash;
//=
operator test if variable is undefined and assign new value when it is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With