Skip to content

In 2D array getting an inner array by variable makes it impossible to get an elem of inner array #766

Description

@Vulwsztyn

Minimal reproducible example:

 (
  $arr := [[], [4, 5]];
  $i := 1; 
  $j := 1; 
  $arr[$i][$j];
)

It should be 1, but I get no match in excerciser.

Image

$arr[1][1]; and $arr[1][$j] work.

Exploring other possibilities:

(
  $arr := [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
  $i := 1;
  $j := 2; 

  $x := $arr[$i];
  {
      "double_variable":$arr[$i][$j],
      "one_variable": $arr[$i],
      "first_variable": $arr[$i][2],
      "second_variable": $arr[1][$j],
      "is_equal": $x[$i] = $arr[$i][$j],
      "separated": $x[$i]
  }
)

results in:

{
  "one_variable": [
    4,
    5,
    6
  ],
  "second_variable": 6,
  "is_equal": false,
  "separated": 5
}

Activity

  1. Turings98apprentice commented on Jan 20, 2026

    @Turings98apprentice
    Contributor

    Edit:
    @Vulwsztyn, it looks like you're just missing a colon...

    Assignment operator is :=.
    Equality operator is =.
    $i is 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 $i is 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 $i resolves to the number 0. I've tried it with other numbers as well, so it's not specific to the number 0 in particular.

    Good catch!

  2. Vulwsztyn commented on Jan 20, 2026

    @Vulwsztyn
    Author

    Thank you @Turings98apprentice I've adjusted the mre

  3. Turings98apprentice commented on Jan 20, 2026

    @Turings98apprentice
    Contributor

    It 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 in v1.1.1 or v1.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"
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions