Accessing Values in an Array



Despite their glamorous lifestyle (for example, in the past year alone Scripting Guys Jean Rossand Greg Stemphave been everywhere from Orlando, FLto … well, Orlando, FL) the Scripting Guys are, at heart, simple people with simple tastes. Take Windows PowerShell, for example. Ask 100 people what they like best about Windows PowerShell and you’ll likely get 100 different answers, most of them dealing with sophisticated new capabilities such as access to the .NET Framework. Ask any of the Scripting Guys what theylike best about Windows PowerShell and you’re likely to get the sameanswer: the cool ways that Windows PowerShell lets you access values in an array.


If you’re an old hand at VBScript then you’ve come to look upon arrays with a certain feeling of dread. Why? Well, for one thing, you can’t just echo back the value of any array; that’s going to result in a “type mismatch” error. For example, take a look at the following VBScript script:


x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Wscript.Echo x


Is that going to work? You already know the answer to that, don’t you? No, it’s notgoing to work. Instead, you need to set up a For Eachor For Next loop in order to get at the value of x:


x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For Each y in x


That’s not particularly hard, but it doesrequire some extra work on your part. On top of that, you might need to check beforehand to ensure that x really isan array in the first place (which results in even morework on your part). After all, trying to loop through something that isn’tan array will also result in an error:


x = 1

For Each y in x


In this case all we’re going to get back is the message “Object not a collection.”


With Windows PowerShell it’s a different story. For example, consider the following Windows PowerShell script:


$x = 1 ,2,3,4,5,6,7,8,9,10


What’s going to happen when we run thisscript? This is what’s going to happen:




In other words, Windows PowerShell lets you access all the values in an array simply by echoing back the array itself (in this case, $x). You don’t have to set up a For Each loop or a For Next loop; PowerShell takes care of all that for you.


Of course, with VBScript you don’t have to access allthe items in an array; you can also access individual items by specifying the item index number. For example, suppose you want to access the third item in the following array:


x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


Because arrays in VBScript (and in PowerShell) are “0 indexed” that means that the very first item has an index number of 0, the second item has an index number of 1, and – by extension – the third item has an index number of 2. How can we access the third item and onlythe third item? By using code like this:


Wscript.Echo x(2)


As you might expect, Windows PowerShell enables you to do the same thing, and with very similar syntax; for example, this code echoes back the value of item 3 in a PowerShell array:


$ x[2]


But that’s not the half of it. Want to echo back the lastitem in the array? That’s possible to do in VBScript, albeit by using some crazy-looking construction similar to this:


Wscript.Echo x( Ubound(x))


Here’s how you accomplish the same task in Windows PowerShell:


$ x[-1]


What’s that? What about the second-to-the-last item in the array? No problem:


$ x[-2]




But wait, there’s more. Suppose you’d like to echo back the value of items having the index numbers 1, 3, 5, and 7? Believe it or not, that’s no problem; just make sure you specify each of those index numbers, separating the individual numbers using commas:


$ a[1,3,5,7]


We’ve saved the very best for the very last. Windows PowerShell features a “numeric range” operator ( ..)that enables you to specify a range of numbers, something that can be very useful when dealing with arrays. Suppose we have an array with 100 items in it, and we need to echo back the value of items 37-79. If we want to, we can list each of those index numbers. Or, we can use the numeric range operator instead:


$ a[37..79]


Nice, huh?To add a little icing to the cake, the range operator isn’t restricted solely to arrays, either. For example, try this little one-line script and see what happens:




Even simple people with simple tastes can’t help but be impressed by that.