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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.