How to properly use .Insert for arrays?

I’m trying to insert an element into an array, however the below code doesn’t seem to work for me.

var MyArray:[]int = array{}

<####>

MyElement:int = 5
if (MyArray.Insert[ 0, MyElement ]):

I think you need to concatenate it like this:
set MyArray = MyArray + array{MyElement }

Ahh yes! However, I’m using Insert as I want to be able to insert MyElement into Index 5, for example!

From the digest:

# Makes a new array by inserting the elements of ElementsToInsert into the Input array such that the index of its first element in the resulting array is InsertionIndex.
    (Input:[]t where t:type).Insert<public>(InsertionIndex:int, ElementsToInsert:[]t)<computes><decides>:[]t = external {}

After scratching my head for a moment, the solution is:

var MyArray:[]int = array{}

<####>

MyElement:int = 5
if (set MyArray = MyArray.Insert[ 0, array{MyElement} ]):
3 Likes

Nicely done, this was messing me up as well. Thanks for sharing.

1 Like