I have an array and I want to push a element at the end of this but I can’t find any form without have to put in the method. How is this possible?
Hi:
Arrays in Verse are immutable (they cannot be changed), but what you can do is assign a new array to the variable you’ve previously assigned the old one to.
Please refer to Array for more information to see how to create a new array with your extra element and assign it to your identifier.
var Array2:[]int = array{20, 21, 22}
set Array2 = Array2 + array{30, 31}
Use := , if needed a fail proof condition, I hav’t started writing verse , just browsing trough docs , try it , let me know.
Seems like appending values to an array is something that can’t fail. Try this:
var Array2:[]int = array{20, 21, 22}
set Array2 += array{30, 31}
As @gontux mentioned above, that is correct, you don’t need to wrap it in a failure context for this. I’ve updated the sample in my original post.
This is odd because by your statement I should not be able to do the following:
set Foo[Index] -= ThisAmount
Where ‘Foo’ is an array of type float and ‘ThisAmount’ is of type float. BUT, when I type this in, no errors appear, but the line seems to not function whatsoever. Does this mean mutable arrays are coming in a future update to Verse? Or maybe a type Vector?
if(set Foo[Index] -= ThisAmount]) should work, but if you’re doing it inside of a for loop, it seems like Verse makes copies to iterate over, so infor(I:=0..Array.Length-1; X:=Array[I]){set Array[I] += 10; Print("{X}")}
It will just print the original array.
But after the for loop, the values should have changed.
How about a multidimensional array? I can’t figure out the right syntax for it, any suggestions?
var MyArray : [][]int = array {}
for(Row := 0..9):
for (Column := 0..9)
if(set MyArray[Row][Column] += array{GetRandomInt(0, 100)}){}