Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Pushing into multidimensional arrays in Sugarcube

This:
<<set $array[$n].push([2,2])>>
throws an error.

Apparently you can't use a variable in the index and use the .push method at the same time? How do I get around this?

Thank you very much!

Comments

  • You can if the $n element exists and if the element already contains an Array.
    <<set $array to [[],[]]>>
    <<set $n to 0>>
    
    <<set $array[$n].push([2,2])>>
    <<set $array[$n+1].push([3,3])>>
    
    nth array element: $array[$n]
    nth+1 array element: <<print $array[$n+1]>>
    arrray: $array
    
  • As usual, the mistake was a stupid misunderstanding on my part. I was putting in too many indices. Thanks!
Sign In or Register to comment.