' How to insert elements into Array the fast/easy way
' Change the data type to suit your needs


SUB ArrayInsert (Array() AS LONG, Element AS LONG, Value AS LONG)
    DIM M AS QMEMORYSTREAM

    M.Position = 0
    '-- Save previous elements
    M.SaveArray(Array(LBOUND(Array)), Element-1)
    '-- Insert new element
    M.Write(Value)
    '-- Save rest of elements
    M.SaveArray(Array(Element), UBOUND(Array) - Element)
    M.Position = 0
    '-- Retrieve them
    M.LoadArray(Array(LBOUND(Array)), UBOUND(Array)+1)
END SUB


DIM A(1 TO 100) AS LONG

A(1) = 10
A(2) = 20
A(3) = 30
A(4) = 40
A(5) = 50
A(6) = 60

ArrayInsert(A, 3, 99)   '-- Insert value 99 in array A at element 3

FOR I = 1 TO 7
    PRINT A(I)
NEXT

PRINT: PRINT "Press any key to continue..."
DO: LOOP UNTIL INKEY$<>""
