Repository navigation
In 2D array getting an inner array by variable makes it impossible to get an elem of inner array #766
Description
Activity
Turings98apprentice commented
on Jan 20, 2026 ContributorMore actionsEdit:
@Vulwsztyn, it looks like you're just missing a colon...Assignment operator is
:=.
Equality operator is=.
$iis never initialized because it's compared to zero instead of being set to zero.( $arr := [1]; $i = 0; /* <<< should be $i := 0; */ $arr[$i]; )I missed this as well. Including my original (and incorrect) comment for posterity. Easy mistake to make!
It looks like what's happening is
$iis being interpreted as a condition to filter the array rather than an index to specify one or more of its elements. Here's an even smaller minimal reproduction example:( $arr := [1]; $i = 0; $arr[$i]; )... results in
null/ "no match" whereas ...( $arr := [1]; $arr[0]; )... results in
1.According to the documentation:
If the square brackets contains a number, or an expression that evaluates to a number, then the number represents the index of the value to select.
So this does appear to be a bug, since
$iresolves to the number0. I've tried it with other numbers as well, so it's not specific to the number0in particular.Good catch!
Reacted by Pedro BiniThank you @Turings98apprentice I've adjusted the mre
Reacted by Turings98apprenticeTurings98apprentice commented
on Jan 20, 2026 ContributorMore actionsIt looks like there's still a bug here, and the new coalesce operator is affected by it as well. I suspect this was a regression introduced in
v1.2.6, because this bug doesn't exist inv1.1.1orv1.0.10, but does exist in every version since.Here's some additions to the minimal reproducible example that might help shed light on what's going on under the hood:
( $arr := [[], [4, 5]]; $i := 1; $j := 1; { '1 (Fail): $arr[$i][$j]': $arr[$i][$j], '2 (Pass): ($arr[$i])[$j]': ($arr[$i])[$j], '3 (Fail): $arr[$i][0][$j]': $arr[$i][0][$j], '4 (Pass): $arr[1][$j]': $arr[1][$j], '5 (Fail): $arr[$i][1]': $arr[$i][1], '6 (Pass): $arr[1]': $arr[1], "7 (Fail): $arr[1] ?? 'fallback'": $arr[1] ?? 'fallback', '8 (Pass): $arr[$i]': $arr[$i], "9 (Fail): $arr[$i] ?? 'fallback'": $arr[$i] ?? 'fallback', '10 (Pass): $arr[1][0]': $arr[1][0], '11 (Fail): $arr[$i][0]': $arr[$i][0], '12 (Pass): $type($arr[1])': $type($arr[1]), '13 (Pass): $type($arr[$i])': $type($arr[$i]), '14 (Pass): $type($arr[1][0])': $type($arr[1][0]), '15 (Fail): $type($arr[$i][0])': $type($arr[$i][0]) } )... yields:
{ "2 (Pass): ($arr[$i])[$j]": 5, "3 (Fail): $arr[$i][0][$j]": 5, "4 (Pass): $arr[1][$j]": 5, "6 (Pass): $arr[1]": [ 4, 5 ], "7 (Fail): $arr[1] ?? 'fallback'": 5, "8 (Pass): $arr[$i]": [ 4, 5 ], "9 (Fail): $arr[$i] ?? 'fallback'": "fallback", "10 (Pass): $arr[1][0]": 4, "11 (Fail): $arr[$i][0]": [ 4, 5 ], "12 (Pass): $type($arr[1])": "array", "13 (Pass): $type($arr[$i])": "array", "14 (Pass): $type($arr[1][0])": "number", "15 (Fail): $type($arr[$i][0])": "array" }Reacted by Artur Mostowski
Minimal reproducible example:
It should be
1, but I getno matchin excerciser.$arr[1][1];and$arr[1][$j]work.Exploring other possibilities:
results in: