Here is an example of how this might be useful:
I have an "INSERT1" function that inserts an element into a one-dimensional array at a specified index. However, since the output of this function is an array with a different size than the input, I have to read the output out of the function after calling the function.
Here is my entire "insert" library (INSERT1, INSERT2, and INSERT3):
Code: Select all
'g'
/*
INSERT1 (a(), iI, nI)
INSERT2 (a(,), d$, iI, nI)
INSERT3 (a(,,), d$, iI, nI)
Functions for inserting elements into numeric arrays.
'==============================================================
NOTE: Because the output of these functions is an array that is a different size than the input array, the resulting array contents will have to be manually copied after calling the function. For example:
DIM data(3, 4) ' data has 3 rows and 4 cols
GET DIM data XSIZE nX YSIZE nY
nIns = 2
INSERT2(data, "x", 1, nIns) ' insert 2 rows at x=1
nX += nIns
DIM data(nX, nY) ' redim data (clears to 0s)
FOR i = 0 TO nX-1 ' copy from INSERT2.array(,)
FOR j = 0 TO nY-1
data(i,j) = INSERT2.array(i,j)
NEXT j
NEXT i
'==============================================================
INSERT1 inserts one or more elements into a one-dimensional numeric array. Inserted elements have a value of 0.
Inputs
------
a() Numeric array
iI Index in a() of insertion
nI Number of elements to insert into a()
'==============================================================
INSERT2 inserts one or more rows or columns into a two-dimensional numeric array. Inserted elements have a value of 0.
Inputs
------
a(,) Numeric array
d$ Dimension of a(,) to insert ("x" or "y")
iI Index in a(,) of insertion
nI Number of rows or cols to insert in a(,)
'==============================================================
INSERT3 inserts one or more rows, columns, or "aisles" into a three-dimensional numeric array. Inserted elements have a value of 0.
Inputs
------
a(,,) Numeric array
d$ Dimension of a(,,) to insert ("x", "y", or "z")
iI Index in a(,,) of insertion
nI Number of rows, cols, or aisles to insert in a(,,)
'==============================================================
*/
''
DEF INSERT1 (a(), iI, nI)
GET DIM a XSIZE x
DIM array(x + nI)
iA = 0
FOR i = 0 TO x-1
IF i = iI THEN iA += nI
array(iA) = a(i)
iA += 1
NEXT i
END DEF
'==============================================================
DEF INSERT2 (a(,), d$, iI, nI)
dX = 0 ! dY = 0
GET DIM a XSIZE x YSIZE y
GET DIM a XSIZE xA YSIZE yA
IF d$ = "x" THEN
xA = x + nI
dX = nI
ELSE ' d$ = "y"
yA = y + nI
dY = nI
END IF
DIM array(xA, yA)
iA = 0
FOR i = 0 TO x-1
IF i = iI THEN iA += dX
jA = 0
FOR j = 0 TO y-1
IF j = iI THEN jA += dY
array(iA,jA) = a(i,j)
jA += 1
NEXT j
iA += 1
NEXT i
END DEF
'==============================================================
DEF INSERT3 (a(,,), d$, iI, nI)
dX = 0 ! dY = 0 ! dZ = 0
GET DIM a XSIZE x YSIZE y ZSIZE z
GET DIM a XSIZE xA YSIZE yA ZSIZE zA
IF d$ = "x" THEN
xA = x + nI
dX = nI
ELSE ! IF d$ = "y" THEN
yA = y + nI
dY = nI
ELSE ' d$ = "z"
zA = z + nI
dZ = nI
END IF ! END IF
DIM array(xA, yA, zA)
iA = 0
FOR i = 0 TO x-1
IF i = iI THEN iA += dX
jA = 0
FOR j = 0 TO y-1
IF j = iI THEN jA += dY
kA = 0
FOR k = 0 TO z-1
IF k = iI THEN kA += dZ
array(iA,jA,kA) = a(i,j,k)
kA += 1
NEXT k
jA += 1
NEXT j
iA += 1
NEXT i
END DEF
'==============================================================
And here is an example of using the library (note the line "data(i,j) = INSERT2.array(i,j)" inside the FOR loops at the end):
Code: Select all
{{/lib/arrays/insert}}
DIM data(3, 4) ' data has 3 rows and 4 cols
GET DIM data XSIZE nX YSIZE nY
FOR i = 0 TO nX-1 ' fill data with some values
FOR j = 0 TO nY-1
data(i,j) = i + j + 1
NEXT j
NEXT i
nIns = 2
INSERT2(data, "x", 1, nIns) ' insert 2 rows at x=1
nX += nIns
DIM data(nX, nY) ' redim data (clears to 0s)
FOR i = 0 TO nX-1 ' copy from INSERT2.array(,)
FOR j = 0 TO nY-1
data(i,j) = INSERT2.array(i,j)
NEXT j
NEXT i
This works because after the function is finished executing, SB remembers the values of all of its variables and allows them to be accessed after the function is called using the notation: funcname.varname