Day 026: ERASE
#1
ERASE probably should be renamed ARRase, because all it erases is the values stored in a specified array.

ERRASE makes the strings assigned to an array null or makes the numeric values assigned to a numeric array zero.

ERASE ArrayName [, others...]

Example:

Code: (Select All)
DIM Pete(10) AS STRING, var(100) AS INTEGER, cnt(20) AS LONG
ERASE Pete, var, cnt

ERASE can be used with STATIC or DYNAMIC arrays, but there is an important difference. Try running the two following code snippets. 

Code: (Select All)
DIM Pete(1 TO 20) AS INTEGER ' DIM makes Pete a STATIC array.
FOR i = 1 TO 20
    Pete(i) = -i
NEXT

FOR i = 1 TO UBOUND(Pete)
    PRINT Pete(i)
NEXT
SLEEP

ERASE Pete

' All zeros will now be output.
CLS
FOR i = 1 TO 20
    PRINT Pete(i)
NEXT
PRINT " ubound(Pete) is still ="; UBOUND(Pete)
Pete(15) = 101
PRINT: PRINT " Pete(15) ="; Pete(15)

Note: This routine will error out unless we Re-initialize the Pete array.
Code: (Select All)
REDIM Pete(1 TO 20) AS INTEGER ' REDIM makes Pete a DYNAMIC array.
FOR i = 1 TO 20
    Pete(i) = -i
NEXT

FOR i = 1 TO UBOUND(Pete)
    PRINT Pete(i)
NEXT
SLEEP

ERASE Pete

' This will error out unless we do a REDIM Pete(1 TO 20) here.
CLS
FOR i = 1 TO 20
    PRINT Pete(i)
NEXT
PRINT " ubound(Pete) is still ="; UBOUND(Pete)
Pete(15) = 101
PRINT: PRINT " Pete(15) ="; Pete(15)

So ERASE appears to have more value and versatility when used with STATIC arrays, if you consider not de-initialing your array as a benefit.

And what makes an array either static or dynamic? Well...

DIM makes the array static.

REDIM makes the array dynamic

And this important note...

REM $DYNAMIC makes ALL arrays dynamic. So even...

Code: (Select All)
REM $DYNAMIC
DIM Pete(1 to 20)
ERASE Pete
REDIM Pete(1 to 20)

...makes the otherwise static DIM array, of Pete, dynamic. So if you use REM $DYNAMIC at the top of your code, use REDIM because a DIM statement after an ERASE statement won't work with REM DYNAMIC in your code.

REM $STATIC makes ALL arrays static. but...

Code: (Select All)
REM $STATIC
REDIM Pete(1 to 20) ' Change to DIM to get this to work.
ERASE Pete
PRINT Pete(15) ' Errors out because even though we used REM $STATIC REDIM messed it up.

So we've kicked the tires quite a bit here. Anyone want to add anything more?

Pete
Reply


Messages In This Thread
Day 026: ERASE - by Pete - 12-07-2022, 07:24 AM
RE: Day 026: ERASE - by mnrvovrfc - 12-07-2022, 10:39 AM
RE: Day 026: ERASE - by bplus - 12-07-2022, 05:20 PM
RE: Day 026: ERASE - by Pete - 12-07-2022, 05:36 PM
RE: Day 026: ERASE - by bplus - 12-07-2022, 08:18 PM
RE: Day 026: ERASE - by Pete - 12-07-2022, 08:31 PM
RE: Day 026: ERASE - by SMcNeill - 12-07-2022, 10:13 PM
RE: Day 026: ERASE - by mnrvovrfc - 12-07-2022, 10:45 PM
RE: Day 026: ERASE - by Pete - 12-07-2022, 10:54 PM
RE: Day 026: ERASE - by sivacc - 06-10-2023, 06:28 PM
RE: Day 026: ERASE - by TerryRitchie - 06-10-2023, 06:31 PM
RE: Day 026: ERASE - by SMcNeill - 06-10-2023, 06:35 PM
RE: Day 026: ERASE - by bplus - 06-10-2023, 06:39 PM



Users browsing this thread: 10 Guest(s)