The repeated expression is only evaluated once, so you are reusing the same anonymous array for each element of the hash. You can map instead of the repetition operator if you want the expression to be evaluated for each repetition.

For example, the problem can be solved by replacing

my %hash3; @hash3{keys(%hash3_origin)} = ([0, 0]) x keys(%hash3_origin);
with
my %hash3; @hash3{keys(%hash3_origin)} = map { [0, 0] } 1..keys(%hash3_origin);

That said, the following are much simpler:

my %hash3; $hash3{$_} = [0, 0] for keys(%hash3_origin);
or
my @hash3 = map { $_ => [0, 0] } keys(%hash3_origin);

In reply to Re: Best way to initialize hash x repetition array? by ikegami
in thread Best way to initialize hash x repetition array? by Veltro

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.