I am reading Damian Conway’s “Perl Best Practices” and found the following code snipet:
$have_reconsidered{lc($name)}++;
I am trying to figure out what is going on here with the hash. I know ++ increments by one in a numeric context, but what does it do to a hash?
From perlop documentation:
undef is always treated as numeric,
and in particular is changed to 0
before incrementing (so that a
post-increment of an undef value will
return 0 rather than undef). The
auto-decrement operator is not
magical.
So in the example above, is the value for the key lc($name) being initialized to 0 and then incremented to 1 by ++?
In general, where could I find out more about the behaviors of ++, +=, etc…?
%have_reconsideredis your hash.$nameis a string.lc($name)returns the lowercased string.$hash{$key}will return the scalar value from hash%hashstored with key$key. so:so, all you do is increment the value in a hash at a given index (namely
lc($name))test case: