In our last VBScript tutorial we covered the string functions that VBScript has to offer. While there aren’t quite as many Array functions as there were with the earlier date functions, never fear: I still haven’t covered the formatting, string, conversion, and other functions. So get ready, because (as usual) we still have plenty of ground to cover.
Contributed by James Payne Rating: / 11 May 05, 2008
If you have made it his far in the series then you no doubt know all about variables and arrays. But just in case, I will briefly cover them here. Feel free to skip to the section labeled VBScript Array Functions if you are familiar with variables and arrays and do not need a refresher. Or if you enjoy the pain, read on!
Variables
Variables are containers in which you store data. In that sense, they're similar to a box. You can put data in them, you can take data out and put it back in, you can take data out and put new data in, or you can delete the data within them. They are called variables because the data within them varies.
You declare variables in VBScript with the Dim statement. Here, we will declare some variables and give them a value:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable
Dim secondVariable
firstVariable="13"
secondVariable="James"
</script>
</body>
</html>
The above code creates two variables, one namedfirstVariableand the other named secondVariable. We then store data in each one (13 and James respectively). Now let's do something with those variables:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable
Dim secondVariable
firstVariable = 13
secondVariable = "James"
document.write("My IQ is " & firstVariable)
document.write("<br />My name is " & secondVariable)
</script>
</body>
</html>
The above code would print the following to the browser:
My IQ is 13
My name is James
You can also declare variables and use them in this manner:
<html>
<body>
<script type="text/vbscript">
Dim firstVariable, secondVariable
firstVariable = 13
secondVariable = "James"
document.write("My IQ is " & firstVariable)
document.write("<br />My name is " & secondVariable)
</script>
</body>
</html>
You get the same result, but save space by declaring the variables on the same line.
Arrays are similar to variables, except where variables hold a value, arrays hold many values. You can think of a variable as a file folder, and an array as a file cabinet. Here is how we declare an array and assign values to it:
<html>
<body>
<script type="text/vbscript">
Dim cow(3)
cow(0) = "Moo "
cow(1) = "Went "
cow(2) = "the "
cow(3) = "cow"
document.write(cow(0))
</script>
</body>
</html>
The above code would result in:
Moo
This is because we told the program to write out the first value in the array, which was moo. You will note that when we Dimmed the array we called "cow," and we put the number 3 in parentheses. This sets the amount of elements, or values, we are storing in the array. I know I know...there are four values in the array, so why did we type three? We do this because array element indexes (the position of the data) begin at 0.
If we had written Dim cow(2), there would be three values, etc.
If we wanted to print out all of the values in the array, we would have to use a For Loop; we will cover this in a later tutorial in more depth. For now, check out the example shown below:
<html>
<body>
<script type="text/vbscript">
Dim cow(3)
cow(0) = "Moo "
cow(1) = "Went "
cow(2) = "the "
cow(3) = "cow"
For Each present In cow
document.write(present)
document.write("<br /><br />")
Next
</script>
</body>
</html>
This would result in the follow being printed to your monitor:
Moo
Went
the
cow
Note that if we had not inserted the <br /> tags, it would have printed...
Moo Went the cow
Lifetime of Variables
A variable's scope is its lifetime, or how long the variable is able to be used. If you put a variable inside a procedure (more on this later), it is only available inside that procedure and when the program exits the procedure, the variable is destroyed. This is known as a local variable. It cannot be used by any other procedures in the program.
A variable declared outside of a procedure is available to the entire program and only dies when the page is closed.
In VBScript you can create arrays with up to sixty dimensions. You'll probably never need to, but you know there is some nerd out there trying to pick up chicks by bragging about how he once created a 60 dimensional array that held every ancillary character from the Phantom Menace. I mean there are those people that build log cabins out of toothpicks to ensure they are remembered after passing over to the dark abyss.
It's far easier to show you how to use multidimensional arrays than to explain them, and since I am a lazy writer, that is exactly what I am going to do:
<html>
<body>
<script type="text/vbscript">
Dim cow(3,2)
cow(0,0) = "Mister Moo "
cow(0,1) = "Moo McGoo "
cow(0,2) = "Bessie "
cow(0,3) = "Elmer"
</script>
</body>
</html>
In the above example we create a multidimensional array, specifically, a two-dimensional one. You will notice that cow(3,2) has two numbers separated by a comma. This is because multidimensional arrays work similar to a grid or a spreadsheet, with the first number representing a row and the second number representing a column. So for instance, in the above code, Bessie appears in row 0 of column 2. You perform actions on multidimensional arrays the same as you would any other array.
Dynamic Arrays
Dynamic arrays are arrays whose size can be altered. To do so, you first initialize the array with no elements, then use ReDim to change its size, like so:
<html>
<body>
<script type="text/vbscript">
Dim cow()
Redim cow(2)
cow(0)="Moo"
cow(1)="Moo Moo"
cow(2)="Moo Moo Moo"
Redim Preserve cow(3)
cow(3)="Moo Moo Moo Moo"
</script>
</body>
</html>
The above code initially creates a dynamic array named cow, with no elements. We then use ReDim to change the number of elements within cow to 3 (cow(2)). Then we add some values to the array. Finally, we decide to add even more elements to cow, and ReDim it again to hold 4 values. We add the fourth value to the array, making sure to use the Preserve keyword. Had we not used Preserve, then all of the data in our array would have been deleted when we Redimmed it.
Simply put, array functions allow you to, well...perform functions on arrays. Mind boggling, I know. For starters, we will begin with the most obvious:
The Array Function
The array function takes a list of comma-separated values and stores them in an array. Here it is at work:
dim myArray
myArray=Array(100, 200, 300, 400, 500)
document.write(myArray(1))
This results in the print out:
200
Remember: the first value in an array has the index number 0.
IsArray?
The IsArray function asks whether something is an array. If it is, it returns a Boolean True; if not, it returns a Boolean False. Here is the science:
If you need to know the size of an array, then you need the UBound and LBound functions. The LBound returns the smallest index number, which of course will always be 0. The UBound returns the largest index number. Note that while UBound might return the highest index number, there may not be a value in that index. Here they are at work:
<html>
<body>
<script type="text/vbscript">
dim super(5)
super(0)="I"
super(1)="Am"
super(2)="Iron"
super(3)="Man"
document.write(UBound(super))
document.write("<br />")
document.write(LBound(super))
</script>
</body>
</html>
This gives the result:
5
0
Filtering Function
You can use the Filter function to filter the items in an array. Let's say you have a list of some He-Man heroes and villains and you want to see all of the ones with the word "man" in their name (there are more than you think):
<html>
<body>
<script type="text/vbscript">
dim grayskull(5),a
grayskull(0)="He-Man"
grayskull(1)="Man at Arms"
grayskull(2)="Skeletor"
grayskull(3)="Evil Lynn"
grayskull(4)="Stinkor"
a=Filter(grayskull,"Man")
document.write(a(0) & "<br />")
document.write(a(1) & "<br />")
document.write(a(2) & "<br />")
document.write(a(3) & "<br />")
document.write(a(4))
</script>
</body>
</html>
This returns the following names:
He-Man
Man at Arms
Now if you are looking for the elements that DO NOT contain "man," you simply add false to the equation, like this:
This function allows us to join the subsection in an array. You combine it with the Filter function like so:
<html>
<body>
<script type="text/vbscript">
dim grayskull(5),a
grayskull(0)="He-Man"
grayskull(1)="Man at Arms"
grayskull(2)="Skeletor"
grayskull(3)="Evil Lynn"
grayskull(4)="Stinkor"
a=Filter(grayskull,"Man",false)
document.write(join(a))
</script>
</body>
</html>
This combines all of the elements that DO NOT contain "Man" (if you wanted all the ones that did contain "Man" you would just leave off the false). Here it is:
Skeletor Evil Lynn Stinkor
I Gotta Split
The Split function splits a variable or array in multiple parts. Here it is in code:
<html>
<body>
<script type="text/vbscript">
dim banana,a
banana="Look ma no hands!"
a=Split(banana)
document.write(a(0) & "<br />")
document.write(a(1))
</script>
</body>
</html>
This returns the following:
Look ma
Or the first two elements in the array.
Well that's it for the Array Functions. If you're good and send me money and praise you will not be subjected to any more Function articles. Otherwise...more will come!