LoginLogin
Might make SBS readonly: thread

Array Resize

Root / Submissions / [.]

12Me21Created:
A function to resize any type of 1 dimensional array:
DEF RESIZE ARRAY,SIZE
 WHILE LEN(ARRAY)>SIZE && ""*POP(ARRAY):WEND
 COPY ARRAY,SIZE,ARRAY,0
END
Example usage:
DIM A[10]
?LEN(A)

RESIZE A,3
?LEN(A)

RESIZE A,27
?LEN(A)

EVEN BETTER:
DEF RESIZE A, S
DIM NEW[S]
COPY A,NEW
A = NEW
OR SOMETHING SIMILIAR

Replying to:DFrost
EVEN BETTER:
DEF RESIZE A, S
DIM NEW[S]
COPY A,NEW
A = NEW
OR SOMETHING SIMILIAR
can you downsize

Replying to:DFrost
EVEN BETTER:
DEF RESIZE A, S
DIM NEW[S]
COPY A,NEW
A = NEW
OR SOMETHING SIMILIAR
Yes, but the point is to do it without having to create a new array. For your solution to work, you'd need to do:
DEF RESIZE(A,S)
 DIM NEW[S]
 COPY NEW,A,S
 RETURN A
END

DIM TEST[10]
TEST=RESIZE(TEST,3)
(Plus you'd need separate functions for int/float/string arrays) Which works fine (and is probably faster) in many situations, but, for example, in a function that modifies arrays in-place, it won't work.