Converting VBScript's Erase Statement

Definition: Reinitializes the elements of fixed-size arrays and deallocates dynamic-array storage space.

Erase

The Erase statement is similar to a check for a million dollars: although we have no doubt that these items exist, the Scripting Guys have little experience with either one. Suppose you have an array with 9 items. When you run the Erase statement against that array you’ll still have an array with 9 items; however, none of those items will have a value (string values will now be empty, while numeric values will be set to 0).

Got that? Like we said, we run across the Erase statement about as often as we run across a million dollars. However, if you need to set all the values in an array to 0, well, here’s a command that can do that for you:

for ($i = 0; $i -lt $a.length; $i++) {$a[$i] = 0}

Let’s assume that $a started off life as an array with the following values:

1
2
3
4
5
6

Here’s what $a looks like after running the preceding command:

0
0
0
0
0
0

Return to the VBScript to Windows PowerShell home page