Login | Register | FAQ
Anonymous

Lesson 8: Arrays

In this lesson we will look at what an array is, how it is used, and what it can do.

Arrays can be a little difficult in the beginning. But give it a try anyway... we've tried to make it as easy as possible.

What is an array?

An array is a set of indexed elements where each has its own, unique identification number.

Sound confusing? It's actually not that complicated.

Imagine a list of words separated by commas. It is called a comma separated list, and it could, for example, look like this:


	apples, pears, bananas, oranges, lemons
	
	

Then try to imagine dividing the list at each comma. Next, give each section a unique identification number like this:

apples (0), pears (1), bananas (2), oranges (3), lemons (4)

What you see is an array. We can, for example, name the array "fruits". The idea is that we can access the array with a number and get a value back, like this:

fruits(0) = apples
fruits(1) = pears
fruits(2) = bananas
fruits(3) = oranges
fruits 4) = lemons

This is the idea behind arrays. Let us try to use it in practice.

How do you use an array?

We will continue with the fruit example. Step by step, we will make it work as a real array. First, we set a variable equal to the list of fruits:


	<%
	Dim fruitlist

	fruitlist = "apples, pears, bananas, oranges, lemons"
	%>
	
	

Next, we use the function DocumentationSplit to split the list at each comma:


	<%
	Dim fruitlist, arrFruits

	fruitlist = "apples, pears, bananas, oranges, lemons"

	arrFruits = Split(fruitlist,",")
	%>
	
	

Voila! "arrFruits" is now an array!

Notice that we called the function DocumentationSplit with two arguments:

  1. the list that should be split
  2. and the delimiter - i.e., the character used to split (in this case a comma) in quotation marks: ",".

Here we use a comma as a delimiter, but you can use any character or word as delimiter.

Let us try to comment the script and put it into an ASP page:


	<html>
	<head>
	<title>Array</title>
	</head>
	<body>

	<%

	' Two variables for the list and the array

	Dim fruitlist, arrFruits

	' Comma separated list
	fruitlist = "apples, pears, bananas, oranges, lemons"

	' Create an array by splitting the list (with comma as delimiter)
	arrFruits = Split(fruitlist,",")

	   ' Write the values from our array

	   Response.Write "<p>The list of fruits:</p>

	   Response.Write "<ul>"

	   Response.Write "<li>" & arrFruits(0) & "</li>"
	   Response.Write "<li>" & arrFruits(1) & "</li>"

	   Response.Write "<li>" & arrFruits(2) & "</li>"
	   Response.Write "<li>" & arrFruits(3) & "</li>"

	   Response.Write "<li>" & arrFruits(4) & "</li>"
	   Response.Write "</ul>"

	%>

	</body>
	</html>
	
	

This example is very simple, and it might be a difficult to see the advantage of using an array for this particular task. But just wait... arrays can be used for many very useful things.

Loop through an array

Back in lesson 5 you learned about loops. Now we will look at how you can loop through an array.

When you know how many elements an array contains, it is not a problem defining the loop. You simply start with 0 and let the loop continue to the number of items available. In the example with the fruits, you would loop through the array like this:


	<html>
	<head>
	<title>Array</title>

	</head>
	<body>

	<%
	' Two variables for the list and the array

	Dim fruitlist arrFruits

	' Comma separated list
	fruitlist = "apples, pears, bananas, oranges, lemons"

	' Create an array by splitting the list (with comma as delimiter)
	arrFruits = Split(fruit list ,",")

	   Response.Write "<p>List of fruits:</p>
	   Response.Write "<ul>"

	   ' Loop through the array arrFrugt

	   For t = 0 to 4
	      Response.Write "<li>" & arrFruits (t) & "</li>
	   Next

	   Response.Write "</ul>"
	%>

	</body>
	</html>
	
	

As you can see, the variable t (which increase from 0 to 4 in the loop) was used to call the array.

How to find the size of an array

But what if we add another fruit to the list? Then our array will contain an element more - which will get the identification number 5. Do you see the problem? Then we need to change the loop, so it runs from 0 to 5, or else not all of the elements will be included.

Wouldn't it be nice if we automatically could find out how many elements an array contains?

That is exactly what we can do with the functions DocumentationUbound and DocumentationLbound, which return respectively the highest and lowest identification number in an array.

With these two functions, we can determine the size of any array. This can be used to create a loop that works regardless of the number of elements:


	<%
	   For t = LBound(arrFrugt) to UBound(arrFrugt)
	      Response.Write arrFruits(t)
	   Next
	%>
	
	

This loop will work regardless of how many or few elements the array contains.

Another example

Below is another example of how you can use an array to write the name of the month:


	<html>
	<head>
	<title>Array<title>

	</head>
	<body>
	<%
	' A variable for the names of the months

	Dim arrMaaneder

	' Note the comma before January - because there is no month with the number 0
	arrMaaneder = Array(,"January","February","March","April","May","June","July","August","September","October","November","December")

	' Call the array with the number of the month - write to the client

	Response.Write arrMaaneder(Month(Date))
	%>
	</body>
	</html>
	
	

Notice that we use the function DocumentationArray instead of the function DocumentationSplit to create an array.

Ok. Enough about arrays! In the next lesson, you will learn how to write your own functions.



<< Lesson 7: Comment your scripts

Lesson 9: Functions >>