in reply to Perl assign scalar to array
Your code fails because it assigns a single scalar value to the lvalue list produced from a hash an array slice. Only one of the elements in the slice will be populated. To populate each element, you can use the x operator in list context.
my @a; @a[2..5] = (2)x4;
...or, using map
@a[2..5] = map 2, 2..5;
...or, using a foreach loop...
$a[$_] = 2 for 2..5;
There are many other ways to do it as well. Just pick the one that is easiest to read.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl assign scalar to array
by ikegami (Patriarch) on Oct 14, 2015 at 19:29 UTC | |
by davido (Cardinal) on Oct 14, 2015 at 23:16 UTC |