in reply to Creating hash: Key is unique string in array element, value is number of times it appears
In the case where $hit[2] is not a key in %genus, requesting the key will create a corresponding value of undef. Incrementing this value will return 1.
so this snippet:
can be reduced to this snippet:#old snippet if (exists ( $genus {$hit[2]})) { $genus{$hit[2]}++; } else{ $genus{$hit[2]} = 1; }
#new snippet $genus{ $hit[2] }++
Example:
http://en.wikipedia.org/wiki/Autovivification%genus = { } ; #genus is empty $genus{ foo }; #requests foo entry, and throws away. #genus is now { foo => undef } $genus{ foo } ++; # genus is now { foo => 1 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Creating hash: Key is unique string in array element, value is number of times it appears
by johngg (Canon) on Oct 26, 2011 at 11:06 UTC |