∟"Erase" Statement - Removing All Elements in an Array
This section provides a tutorial example on how to use a 'Erase' statement to reset all elements to the default value in a fixed-size array, or truncate the size of a dynamic-size array to zero.
It is interesting to know that VBScript offers a special statement called "Erase" to remove all values
in an array:
Erase array_variable
If the specified array is a fixed-size array, it will reset all elements to the default value: "Empty".
If the specified array is a dynamic-size array, it will reset the array to zero size.
To show you how an "Erase" statement works, I wrote the following example, array_erase.html:
<html>
<body>
<!-- array_erase.html
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
' Setting a fixed-size array
Dim aFixed(1)
aFixed(0) = "Dog"
aFixed(1) = "Cat"
document.writeln("")
document.writeln("Favorite pets:")
For Each sItem In aFixed
document.writeln(" " & sItem )
Next
' Setting a dynamic-size array
Dim aDynamic()
ReDim aDynamic(1)
aDynamic(0) = "Apple"
aDynamic(1) = "Orange"
document.writeln("")
document.writeln("Favorite fruits:")
For Each sItem In aDynamic
document.writeln(" " & sItem )
Next
' Erasing arrays
document.writeln("")
document.writeln("Erasing arrays...")
Erase aFixed
Erase aDynamic
' Looking at arrays again
document.writeln("")
document.writeln("Pets left:")
iCount = 0
For Each sItem In aFixed
iCount = iCount + 1
document.writeln(" " & sItem )
Next
document.writeln(" Count = " & iCount)
document.writeln("")
document.writeln("Fruits left:")
iCount = 0
For Each sItem In aDynamic
iCount = iCount + 1
document.writeln(" " & sItem )
Next
document.writeln(" Count = " & iCount)
</script>
</pre>
</body>
</html>
Here is the output:
Favorite pets:
Dog
Cat
Favorite fruits:
Apple
Orange
Erasing arrays...
Pets left:
Count = 2
Fruits left:
Count = 0