Append to Arrays
-
Hi!
Is there any way to append items to an array while the game is running (i.e. with a command, rather than typing a new item into it yourself)?
An example of what I mean, where 'append' means 'add onto the end of the array' (this is just a guess at how it could be done):
nums = [] boop = 0 loop if boop < 6 then nums append boop else boop = 0 endif boop += 1 repeat
-
-
@Jack_Blue
Something like:
nums[len(nums)] = boop
-
Great minds, etc.
-
Thanks, both!
-
Just a few notes:
As far as I know you can add a value to an existing array only if you add it in the next available index. So if you have an array of length 3, you can only add with a[3] = newVal. Doing a[10] = newVal would not work.
It is also not possible to expand an array by inserting a value at a specific index in the array. You would have to write a looping function for this yourself If you need it.
-
I think it does work to extend the array however the unassigned values are void which is one way to get the "Unrecognised type: 4" error message. So if you do a[10] = newVal when the array length was 3, a[3] to a[9] will exist but have undefined values.
-
Ah, OK. I only know that its been giving me grief in the past, so I have made a habit of only extending it one index at the time.
-
@vinicity Probably safest!
-
Looks like it’s time to write an append function then!
-
Just to be clear, btw, I’m not going to do it.
-
@toxibunny yeah please i also really need one