Consider:
%hash; # a hash $hash{$foo} = 'bar'; # used in scalar context @ary = qw(foo bar baz); $hash{@ary}++; # key is scalar(@ary) here: $hash{3} == 1; @hash{@ary} = (1,2,3); # hash is now (foo=>1,bar=>2,baz=3) @hash{@ary}++; # $hash{baz} == 4; # why?
What you want is
@hash{@array} = (1) x scalar(@array);
If you use a hash slice, you use the hash in list context. So assign it a list.
<update> If you say @hash{@ary}++, the hash iterator points to the hash key corresponding to the last element of @ary after the slice has been set up. The value of that key is incremented. </update>
<update> fixed typo (better: bug, negligence) at x - thanks cdarke ;-)
fixed another typo in first code block </update>
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: postfix incrementing of hash slice
by shmem
in thread postfix incrementing of hash slice
by mjl69
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |