perl - Initialize hash elements to 0 -
i declare hash in perl doing this:
my %hash = ();
i go on adding elements hashes. sometimes, $hash{$x}
not defined, meaning null
. when try print it, not anything. expect see 0
in case entry $x
not defined. can tell me how do this? how initialize hash elements 0
initially?
instead of trying set default value, can print default value when encounter undefined value using defined-or operator, //
(works perl 5.10 , higher).
in example, when print hash elements, either print element, or if not defined, 0
:
use 5.010; $hash{$x} // 0;
Comments
Post a Comment