VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

∟"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

I don't see any surprises in the output. Do you?

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

►Array Data Type and Related Statements

 What Is an Array?

 "Dim x()" - Declaring Array Variables

 "x(i)" - Accessing Array Elements with Indexes

 "Dim x(n)" - Fixed-Size Array Example

 "Dim x()" - Dynamic-Size Array Example

 "For Each" Statement Example

►"Erase" Statement - Removing All Elements in an Array

 Data Type "Variant()" - Array of Variant Values

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"Erase" Statement - Removing All Elements in an Array - Updated in 2015, by Dr. Herong Yang