in reply to Re^3: Array wierdness
in thread Array wierdness

Hello,

you can also initialize an ArrayOfArrays setting default values using x (repetition operator in list context) like in:  @aoa = ([( 1 ) x 3]) x 3

L*

PS I evidently need more coffeine this morning.. see below

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^5: Array wierdness -- initialize AoA
by hv (Prior) on Apr 26, 2022 at 09:13 UTC

    That's usually not what you want: that'll give you three copies of the same arrayref, so if you later modify an element in one row it'll modify it in all rows:

    % perl -wle '@aoa = ([(1) x 3]) x 3; $aoa[1][2] = 3; print "@$_" for @ +aoa' 1 1 3 1 1 3 1 1 3

    If you want each element to be independent, a typical approach is to pass it through map:

    % perl -wle '@aoa = map [(1) x 3], 1..3; $aoa[1][2] = 3; print "@$_" f +or @aoa' 1 1 1 1 1 3 1 1 1