VBScript Arrays
Imagine that you would like to store a list of all the gifts you would like
to receive on your wedding day. You want to make a web page that displays a list
of all the items. If you were to create a variable
for each gift then you might end up having 100 or more variables for gifts alone!
However, there is a better solution to this engineering problem.
Instead, you could utilize arrays, which allow you to store many variables(elements) into
a super variable (array). Each present would have a position in the array, starting from
position 0 and ending with the last gift.
VBScript Creating an Array
We are going to dumb down the example a little bit so that this lesson doesn't get
too boring. Let's imagine that we have 4 gifts we want to store in our array. First
we need to create an array to store our presents and tell VBScript how big we want our
array to be.
As we mentioned, an array's beginning position is 0, so if we specify an array of size
3 that means we can store 4 presents (positions 0, 1, 2 and 3)! This is often confusing
for first time VBScript programmers. Below is the correct code to create a VBScript array
of size 3.
VBScript Code:
<script type="text/vbscript">
Dim myArray(3)
</script>
VBScript Arrays: Storing Data
Now that we have created our array we can begin storing information into it. The way
to do this is similar to setting the value of a variable, but because an array can hold many values
you have to specify the position at which you want the value to be saved.
We have four presents that we need to store and we make sure that we don't store two
presents in the same position!
VBScript Code:
<script type="text/vbscript">
Dim myArray(3)
myArray(0) = "Clean Underwear"
myArray(1) = "Vacuum Cleaner"
myArray(2) = "New Computer"
myArray(3) = "Talking Bass"
</script>
VBScript Arrays: Accessing Data
We have all the data stored into the array that we need, now we need to figure
out how to get it back out so we can print it to the web page! This step is
nearly identical to the storing phase because you have to specify the position
of the element you wish to display. For example, if we wanted to print out
the value of the present at position 0 in our array you would use the following code:
VBScript Code:
<script type="text/vbscript">
Dim myArray(3)
myArray(0) = "Clean Underwear"
myArray(1) = "Vacuum Cleaner"
myArray(2) = "New Computer"
myArray(3) = "Talking Bass"
document.write(myArray(0))
</script>
Display:
Clean Underwear
VBScript Arrays: Accessing All Data
The above example was a good introduction to accessing elements in an array, but
it isn't that helpful for printout out all items that might be in an array. If
we had 300 items in our array, accessing them one by one would be most time consuming
to program.
Below is a piece of code that will automatically go through every
element in the array and print it out. The special programming structure this
example uses is a For Loop that we will be discussing in greater detail later on
in this tutorial.
VBScript Code:
<script type="text/vbscript">
Dim myArray(3)
myArray(0) = "Clean Underwear"
myArray(1) = "Vacuum Cleaner"
myArray(2) = "New Computer"
myArray(3) = "Talking Bass"
For Each present In myArray
document.write(present)
document.write("<br />")
Next
</script>
Display:
Clean Underwear
Vacuum Cleaner
New Computer
Talking Bass
Found Something Wrong in this Lesson?Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time! |