? Array insert and remove functions ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password
Another contest? You asked for it!

Array insert and remove functions

These will work on any type of 1D array.

Insert

DEF INSERT ARRAY[],POS%,VALUE UNSHIFT ARRAY,VALUE 'This is just to make the array longer. I use VALUE since it will be the same variable type as the array. COPY ARRAY,ARRAY,1,POS% 'Moves all the elements before POS% 1 space to the left. ARRAY[POS%]=VALUE 'Add the new value. END

Remove

DEF REMOVE ARRAY[],POS% COPY ARRAY,POS%,ARRAY,POS%+1,LEN(ARRAY)-(POS%+1) 'move all elements after POS% 1 space left. IF POP(ARRAY) THEN ENDIF'remove the last element. END
Author
12Me21
Updated
Rating
5 votes
Categories
Keywords
3 Comment(s) Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies I have found this so useful, and have come back to this resource numerous times. It is much more efficient than looping through every element. I'm putting this minified code here for future reference, also because INSERT is 0.0006 milliseconds faster and REMOVE is 0.0012 milliseconds faster, that's right, faster than 12Me21 'INSERT AN ITEM INTO AN ARRAY DEF INSERT A[],I%,V UNSHIFT A,V COPY A,A,1,I% A[I%]=V END 'REMOVE AN ELEMENT FROM AN ARRAY DEF REMOVE A[],I% COPY A,I%,A,I%+1,LEN(A)-I%-1 I%=POP(A) END 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself The remove function won't work with string array, but yes, that version is faster for integer or float arrays. Simeon Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies Oh yeah... you're right