Learn about Windows PowerShell
Hey, Scripting Guy! I know I can use the Split function to split a line into individual items. But how can I tell how many “splits” I ended up with; that is, how many individual items did I generate from a single line?-- SA
Hey, SA. For those of you who are relative newcomers to scripting (or those of you who’ve never used the Split function) let’s start off with a brief tutorial. As SA notes, the Split function lets you take a text string of one or more items (with individual items separated by a common delimiter, such as a comma) and then create an array of individual items. For example, suppose we have this line of text:
By running the Split function and telling the function that the comma is our delimiter (that is, the character that separates the individual items) we can easily separate this string into its constituent parts:
All in all a very handy little function, especially when working with file paths, Active Directory AdsPaths, and anything else where the parts are often-times every bit as important as the whole. (For example, the Split function can take a file path like C:\Scripts\MyScript.vbs and separate out the drive, folder, and file name for us.)
All well and good. But SA would like to know how many items were found in an individual text string; for example, we don’t want to know the names of the Seven Dwarves, we just want to know that seven dwarves were listed in the string.
Note. At the risk of bragging, we might point out that the Scripting Guys - who are often mistaken for the Seven Dwarves - knew all seven names without looking them up. And here’s something for you trivia buffs: Dopey (the dwarf most often confused with the Scripting Guys) was originally named Deafy. Strange but true!
So how can we determine the number of items found in the string? Well, we just use a simple little script like this:
strNames = "Dopey,Sneezy,Doc,Grumpy,Sleepy,Happy,Bashful"
arrNames = Split(strNames, ",")
Wscript.Echo Ubound(arrNames) + 1
In line 1 we assign the names of our seven dwarves (each name separated by a comma) to a variable named strNames. In line 2 we call the Split function and indicate that the delimiter is the comma. That’s why you see “,” as the second parameter passed to Split; the first parameter, of course, is the string we want Split to work against. This gives us back an array named arrNames, with each item in the array representing the name of one of the dwarves. (Incidentally, the name arrNames is purely arbitrary; you can use any variable name you want.)
That brings us to line 3:
Wscript.Echo Ubound(arrNames) + 1
What we’re doing here is echoing the Ubound (upper bound) value of our array, plus 1. What does that mean? Well, in an array each item is automatically assigned an index number: the first item is assigned index number 0, the second item is assigned index number 1, and so on. In memory, our array looks something like this:
The Ubound function will always return the index number of the last item in the array; in this case, Ubound returns a 6. The Ubound value will always be one less than the number of items in the array; remember, we actually have seven items in our array. This occurs because the first item in the array is assigned an index number of 0; if the first item was number 1 then the Ubound value would also represent the total number of items in the array. But that’s OK: all we have to do is take the Ubound value, add 1 to it, and we’ll have our total number of items. Case closed!
Oh, and one other thing: yes, we will strongly discourage Peter from singing a scripting-related version of Someday My Prince Will Come during his next webcast. You have our word on it.