in reply to Best way to initialize hash x repetition array?
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
withmy %hash3; @hash3{keys(%hash3_origin)} = ([0, 0]) x keys(%hash3_origin);
my %hash3; @hash3{keys(%hash3_origin)} = map { [0, 0] } 1..keys(%hash3_origin);
That said, the following are much simpler:
ormy %hash3; $hash3{$_} = [0, 0] for keys(%hash3_origin);
my @hash3 = map { $_ => [0, 0] } keys(%hash3_origin);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Best way to initialize hash x repetition array?
by Veltro (Hermit) on Jun 29, 2018 at 21:49 UTC | |
by BrowserUk (Patriarch) on Jun 30, 2018 at 04:28 UTC | |
by Veltro (Hermit) on Jun 30, 2018 at 07:28 UTC | |
by ikegami (Patriarch) on Jun 30, 2018 at 18:07 UTC | |
by ikegami (Patriarch) on Jun 30, 2018 at 18:06 UTC |