| << 4.8.0- Arrays | Chapter4 | 4.8.2- Multi-Dimensional Arrays >> |
Declaring Arrays
Arrays are declared in the same way as variables, using the Dim keyword. However, an array declaration needs a parameter at the end, which is used to specify the size of the array. We could set up an array to have 50 entries for each of the states in the US, with the following statement:
Dim StatesInUS(49)
The index number 49 isn't a mistake. It is just that arrays count from zero upwards in VBScript, rather than from one. So in this case the 50 states are indexed by the 50 different parameter values 0, 1, …, 49. This type of array is known as a fixed-size array, because you fix the maximum number of items that the array can contain. If you don't know the number of items there are to be in your array, or don't wish to specify, you can create a dynamic array instead:
Dim BritishAthlecticsWorldChampionshipWinners()
You can then go back and specify how many items there should be at a later point, using the keyword Redim, which is short for re-dimension, although there is a performance penalty for using Redim, so try to find the number if possible first.
Redim BritishAthleticsWorldChampionshipWinners(0)
There was only one (sniff)…
Redeclaring Arrays
Sometimes there are situations when you've already specified the number of items in a dynamic array, but then you have to amend that amount. If, for example, you'd estimated the number of items but then found that you'd needed a larger array than first envisaged, you could use the Redim keyword once again, to set up the array with the new amount:
Dim amoeba()
Redim amoeba(1)
amoeba(0) = "Geronimo"
amoeba(1) = "Geronimee"
'amoebas divide...
Redim amoeba(3)
However, you'd lose the information already held in the existing array, so you'd have to reenter the information. Fortunately, help is at hand, in the form of another keyword, Preserve, which can be used together with Redim, to ensure that the existing contents of your array are not irretrievably lost:
Redim Preserve amoeba(3)
amoeba(2) = "Geronimo Mk II"
amoeba(3) = "Geronimee Mk II"
Now we're ready to a look a small example utilizing some of these concepts.
Try It Out – Setting Up An Array
In this example, we're going to set up a dynamic array, add some contents, and then display the contents. Then we're going to resize the array, add an extra item, and redisplay it just to prove that we haven't destroyed the original contents
1. Give your ASP editor a poke in the ribs to wake it up, and then type in the following:
<HTML>
<HEAD>
<TITLE>Using Arrays</TITLE>
</HEAD>
<BODY>
<P>Here are the Marx Brothers: </P>
<%
Dim strMarx()
Redim strMarx(4)
strMarx(0) = "Groucho"
strMarx(1) = "Harpo"
strMarx(2) = "Chico"
strMarx(3) = "Zeppo"
strMarx(4) = "Gummo"
Response.Write "<P><B>"
For intCounter = 0 to 4
Response.Write strMarx(intCounter) & "... "
Next
Response.Write "</B></P>"
Response.Write "<P>Whoops! Nearly forgot the bearded leftish one... </P>"
Redim Preserve strMarx(5)
strMarx(5) = "Karl"
Response.Write "<P><B>"
For intCounter = 0 to 5
Response.Write strMarx(intCounter) & "... "
Next
%>
</B></P>
</BODY>
</HTML>
2. Save it as array.asp and close it down
3. Get your browser up and ready and run this program on it:
|
|
How It Works
This program starts by setting up a dynamic array strMarx, and then declaring that it contains five items (in a zero-based array – hence the count from 0 to 4):
<%
Dim strMarx()
Redim strMarx(4)
We then populate our array with five values, the names of the Marx Brothers:
strMarx(0) = "Groucho"
strMarx(1) = "Harpo"
strMarx(2) = "Chico"
strMarx(3) = "Zeppo"
strMarx(4) = "Gummo"
To display the contents of our array, rather printing out each one individually, we have to use a special construct known as a loop. We're going to look at loops in detail in the next chapter . For the time all you need to know is that it is an automatically incrementing counter that sets the value of intCounter to 0, in the first instance and runs the Response.Write line. Then the key aspect of the loop is once it reaches the word Next, it goes back to the beginning of the loop and runs the lines in between For and Next again, but this time setting intCounter to 1. It continues this repetition until the value of intCounter reaches the end of the range assigned in the For statement:
For intCounter = 0 to 4
In this way the loop increments the variable intCounter to go through each item in our array. The loop goes through five values, 0, 1, 2, 3 and 4 and substitutes the value in intCounter on each pass of the loop:
Response.Write strMarx(intCounter) & "... "
Next
The first item in the array, strMarx(0), contains the string "Groucho", and this is displayed on the screen. The next statement tells the loop to go back and add another 1 to the value of intCounter. This time strMarx(1) is displayed, which contains the string "Harpo", that we assigned earlier. It's repeated for strMarx(2) and strMarx(3), and on the final iteration of the loop, the value of 4 is assigned to intCounter which enables the contents of the variable strMarx(4) to be displayed.
However, the program doesn't end there. Next, we display some more HTML, before going back into ASP and resizing the array so that it may hold six items. The keyword Preserve ensures that none of the original items are lost:
Response.Write "<P>Whoops! Nearly forgot the bearded leftish one... </P>"
Redim Preserve strMarx(5)
We set up a value for the new item in our dynamic array:
strMarx(5) = "Karl"
Then we go around the loop again, outputting the contents of the array, except that this time the loop is iterated six times, not five:
For intCounter = 0 to 5
Response.Write strMarx(intCounter) & "... "
Next
If we were to omit the keyword Preserve when resizing the dynamic array, we would lose the names of the five original Marx brothers, so the array would be empty again before adding their political namesake. Try running the program again without the word Preserve, just to see.
| << 4.8.0- Arrays | Chapter4 | 4.8.2- Multi-Dimensional Arrays >> |

RSS


