Removing Items FromArrays

 

 

There’s no doubt that arrays are incredibly useful when writing system administration scripts. There’s even less doubt (assuming you can haveless doubt than no doubt) that Windows PowerShell makes it very easy to work with arrays; to that end, we’ve talked about different tricks you can do with arrays in several of our previous Tips of the Week. Nevertheless, the array class built into Windows PowerShell does have at least one weakness: as easy as it might be to add a new item to an array, there’s no comparably-easy way to remove an existing item from an array. That’s a shame, but, then again, that’s just the way it goes. After all, you have to use the array class built right into Windows PowerShell, don’t you?

 

Using the .NET Framework ArrayList Class

 

You already figured out the answer to our question, didn’t you? Do you haveto use the array class built into Windows PowerShell? Of course not.After all, Windows PowerShell provides complete access to the .NET Framework, and the .NET Framework offers all sorts of alternative arrays, collections, and hash tables. Don’t like the array class built into Windows PowerShell? Then just use a different array type.

 

Good point: whichdifferent array type are we supposed to use? Well, one array type that’s worth investigating is the System.Collections.ArrayList class. To use this array type, all we have to do is use the New-Objectcmdlet to create a new instance of the ArrayListclass, an instance  that we’ve named $a:

 

$a = New-Object System.Collections.ArrayList

 

That command gives us an empty array named $a. If we then want to populate that array with some information (which we probably do), all we have to do is call the Addmethod followed by the item we want to add to the array. For example, here is a series of commands that adds six different color names to our array:

 

$ a.Add ("red")
$ a.Add("yellow")
$ a.Add("orange")
$ a.Add("green")
$ a.Add("blue")
$ a.Add("purple")

 

If we now echo the value of $a, we’ll get back the following:

 

red
yellow
orange
green
blue
purple

 

Pretty cool, huh? Oh; we see. You don’t seem to be very impressed; maybe that’s because, so far anyway, we haven’t really done much. But don’t worry; that’s about to change.

 

To begin with, let’s see if we can remove a specified item from our array. With the standard Windows PowerShell array class that’s a difficult proposition, at best; as the Windows PowerShell help documentation states:

 

It is not easy to delete elements from an array, but you can create a new array that contains only selected elements of an existing array. For example, to create the $t array with all elements in the $ aarray, except for the value at index position 2, type:

 

$t = $ a[0,1 + 3..($ a.length- 1)]

 

Admittedly, that works (assuming you’ve set up the array $a). On the other hand, it’s not particularly intuitive, and relies on you knowing the index number of the item you want to remove; you can’t simply say, “Hey, $a: can you remove the color yellowfor me?” And yet, that’s pretty much all you need to do when working with the ArrayListclass:

 

$ a.Remove ("yellow")

 

As you can see, that’s about as simple a command as you’ll ever use: all you do is call the Removemethod, tacking on the item to be removed as the sole method parameter. Nowtake a look at the value of $a; notice anything missing?

 

red
orange
green
blue
purple

 

That’s right: yellowhas been removed, and without us doing anything more complicated that calling the Remove method. Now that iscool.

 

Here’s another way to quickly and easily remove items (yes, we said items) from an array. Suppose we have our original array, the one with the six different colors in it. For some reason, we’ve decided to keep the first three colors (red, yellow, and orange) and toss out the last three colors. How do we do that? Like this:

 

$ a.RemoveRange (3,3)

 

What we’ve done here is call the RemoveRange method, a method that enables us to delete a range of items from an ArrayList. Notice that we passed RemoveRangea pair of parameters, in this case a pair of 3s. The first 3 represents the index number of the first item to be removed. Like most arrays, the first item in an ArrayListhas an index number of 0; that means that the second item has an index number of 1, and the third item has an index number of 2. Does that matter to us? You bet it does; after all, that means the fourth item (the first item to be removed) has an index number of 3. Hence the first 3, which tells RemoveRangethat the first item we want to delete is the item with the index number 3 (the fourth item in the array).

 

So then what’s the second3 for? That’s easy: that’s number of items we want to remove. Suppose we only want to remove items 4 and 5, leaving item 6. In that case, we’d use this command:

 

$ a.RemoveRange (3,2)

 

That would make $ aequal to this:

 

red
orange
yellow
purple

 

Incidentally, you can determine the number of items in an array simply by echoing back the value of the Countproperty, like so:

 

$ a.Count

 

Oh, one more thing: what if you to get rid of allthe items in the array? Here’s one thought; call the Clearmethod:

 

$ a.Clear ()

 

And that’s just one of the many alternative arrays, collections, and hash tables available in the .NET Framework. Next week we’ll talk about another such alternative: the hash table.