ERASE

From QB64 Phoenix Edition Wiki
Revision as of 17:41, 23 April 2026 by RhoSigma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The ERASE statement is used to clear all data from an array.


Syntax

ERASE arrayName [, arrayName2 ...]
ERASE parentArray(index).subordinateNestedArray


Description

  • You do not have to include the array brackets in an ordinary ERASE call, but you must specify the parent index to clear a subordinary array.
  • Multiple arrays can be erased using commas between the array names for ordinary arrays only.
  • Static arrays will always keep their dimensions and bounds, only the values in the array (contents) are cleared as follows:
    • All numerical array elements become zero(0).
    • All chars of fixed length STRING array elements are filled with CHR$(0).
    • Variable length STRINGs will be emptied ("").
  • Dynamic arrays otherwise are completly destroyed by ERASE, i.e. all its contents is gone and the memory associated with the array is freed.
    • Dynamic arrays must be REDIMed if they are referenced after being erased.
    • You may reduce the memory impact of your program by erasing no longer needed dynamic arrays.

Notes on the second Syntax

QB64-PE v4.5.0 extends ERASE support for arrays nested inside TYPE definitions. Besides the traditional use on ordinary arrays, ERASE may now also be used on an individual nested array member, or on a parent array/variable containing such members. Erasing the parent also clears all subordinate nested arrays. Since arrays inside TYPEs are always static arrays, their dimensions and bounds remain unchanged, only the contents is reset.


Availability

  • Since arrays in TYPEs were implemented in QB64-PE v4.5.0, this statement was adapted to properly handle it (see Example 2).


Examples

Example 1
The classical use on ordinary arrays.
DIM NumArr(5) AS INTEGER
DIM FixArr(5) AS STRING * 3
DIM VarArr(5) AS STRING
NumArr(1) = 123
FixArr(1) = "ABC"
VarArr(1) = "XYZ"

PRINT "Numeric before ERASE: "; NumArr(1)
PRINT "FixStr  before ERASE: "; FixArr(1), "LEN = "; LEN(FixArr(1))
PRINT "VarStr  before ERASE: "; VarArr(1), "LEN = "; LEN(VarArr(1))
PRINT

ERASE NumArr, FixArr, VarArr

PRINT "Numeric  after ERASE: "; NumArr(1)
PRINT "FixStr   after ERASE: "; FixArr(1), "LEN = "; LEN(FixArr(1))
PRINT "VarStr   after ERASE: "; VarArr(1), "LEN = "; LEN(VarArr(1))
PRINT

PRINT "Note that the lenght of the fixed length STRING is unchanged,"
PRINT "it's now just filled with CHR$(0) chars, which don't print."

Example 2
The new usage on subordinary and nested arrays.
TYPE TestType
    aaa(2) AS DOUBLE
END TYPE

DIM a(1) AS TestType
DIM x AS INTEGER
DIM y AS INTEGER

PRINT "Generating values..."
FOR y = 0 TO 1
    FOR x = 0 TO 2
        a(y).aaa(x) = (x + 1) * (y + 1)
        PRINT a(y).aaa(x);
    NEXT x
NEXT y
PRINT
PRINT

PRINT "Erasing only one nested member array: a(0).aaa"
ERASE a(0).aaa

FOR y = 0 TO 1
    FOR x = 0 TO 2
        PRINT a(y).aaa(x);
    NEXT x
NEXT y
PRINT
PRINT

PRINT "Generating values again..."
FOR y = 0 TO 1
    FOR x = 0 TO 2
        a(y).aaa(x) = (x + 1) * (y + 1)
    NEXT x
NEXT y

PRINT "Erasing the parent array: a"
ERASE a

FOR y = 0 TO 1
    FOR x = 0 TO 2
        PRINT a(y).aaa(x);
    NEXT x
NEXT y
PRINT
Example by Petr
Explanation
 In this example, aaa() is a array member inside TestType, and a()
 is an array of TestType elements. ERASE a(0).aaa clears only that
 nested member array. ERASE a clears the parent array and all nested
 array members it contains. This extends the original use of ERASE.
 Since arrays inside TYPEs are always static arrays, their dimensions
 and bounds remain unchanged, only the contents is reset.

See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link