Home > ASP > All Definitions and Usages of Arrays in ASP

All Definitions and Usages of Arrays in ASP

Arrays are one of the basics of programming. We can create lots of things via arrays like matrices, databases, tables… Let’s make it shorter and start.

Array() Function in ASP

We define our arrays using the Array() function. We can store values with two different ways. We can create the array and put values in array at the same time, or we can create the array then we can initialize the values. For getting the values from arrays we have to know position of value in array. For example; second value of array, third value of array, 56th value of array etc. There is very important point that you have to know: Array() function is zero based. That mean’s first value of array is the 0th element of array.

1.1) Fixed Size Arrays

It’s better to use this usage, if you know exactly number of elements which will be used, because the compiler will know the size of array and make the processes faster.

Example 1.0: Creating the array and initializing the values at the same time.

1
2
3
4
5
6
7
8
9
<%
dim my_array
my_array=Array("First","Second","Third","Fourth")

Response.Write "my_array(0) : "&my_array(0)&"<br/>"
Response.Write "my_array(1) : "&my_array(1)&"<br/>"
Response.Write "my_array(2) : "&my_array(2)&"<br/>"
Response.Write "my_array(3) : "&my_array(3)
%>

Output of Example 1.0:

1
2
3
4
my_array(0) : First
my_array(1) : Second
my_array(2) : Third
my_array(3) : Fourth

This is a fixed array that includes only 4 elements that you define. You can not add another element in this array. Also, there is another way to create a fixed array.

Example 1.1: Defining array and size of it, THEN initializing the values.

1
2
3
4
5
6
7
8
9
10
11
12
13
<%
dim my_array(3)
'3 is number of last element. Totally, there are 4 elements.
my_array(0)="Bruce Willis"
my_array(1)="Josh Hartnett"
my_array(2)="Morgan Freeman"
my_array(3)="Sir Ben Kingsley"

Response.Write "my_array(0) : "&my_array(0)&"<br/>"
Response.Write "my_array(1) : "&my_array(1)&"<br/>"
Response.Write "my_array(2) : "&my_array(2)&"<br/>"
Response.Write "my_array(3) : "&my_array(3)
%>

Output of Example 1.1:

1
2
3
4
my_array(0) : Bruce Willis
my_array(1) : Josh Hartnett
my_array(2) : Morgan Freeman
my_array(3) : Sir Ben Kingsley

But, there is a disadvantage of this usage. When we define the array, size of array and values in one time, we can not add any element later. So, you have to use dynamic size arrays.

1.2) Dynamic Size Arrays

Sometimes we need to change the size of array and add new elements into array. That’s why, we use dynamic size arrays. All we need to do, just using redim keyword to resize the array when we add a new element.

Example 1.2: Creating a dynamic array, THEN initializing the values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%
dim my_array() 'We must not define the size of array at first.
redim my_array(3) 'we set the size 4, for now.
my_array(0)="Bruce Willis"
my_array(1)="Josh Hartnett"
my_array(2)="Morgan Freeman"
my_array(3)="Sir Ben Kingsley"
'The all values were set, but we need to add two more records.
'So, we use redim keyword again.
redim my_array(5)
'From now on, the number of last element can be 5.
my_array(4)="Lucy Liu"
my_array(5)="Stanley Tucci"

Response.Write "my_array(0) : "&my_array(0)&"<br/>"
Response.Write "my_array(1) : "&my_array(1)&"<br/>"
Response.Write "my_array(2) : "&my_array(2)&"<br/>"
Response.Write "my_array(3) : "&my_array(3)&"<br/>"
Response.Write "my_array(4) : "&my_array(4)&"<br/>"
Response.Write "my_array(5) : "&my_array(5)
%>

Output of Example 1.2:

1
2
3
4
5
6
my_array(0) :
my_array(1) :
my_array(2) :
my_array(3) :
my_array(4) : Lucy Liu
my_array(5) : Stanley Tucci

As you se, the 4 records at the beginning are removed because of redim keyword. However, we want to use them too. So what can we do? We can use Preserve keyword.

Example 1.3: Importance of PRESERVE keyword.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%
dim my_array()
redim my_array(3)
my_array(0)="Bruce Willis"
my_array(1)="Josh Hartnett"
my_array(2)="Morgan Freeman"
my_array(3)="Sir Ben Kingsley"
'we use redim keyword again to resize the array.
Redim PRESERVE my_array(5)
'We don't lose our values anymore.
my_array(4)="Lucy Liu"
my_array(5)="Stanley Tucci"

Response.Write "my_array(0) : "&my_array(0)&"<br/>"
Response.Write "my_array(1) : "&my_array(1)&"<br/>"
Response.Write "my_array(2) : "&my_array(2)&"<br/>"
Response.Write "my_array(3) : "&my_array(3)&"<br/>"
Response.Write "my_array(4) : "&my_array(4)&"<br/>"
Response.Write "my_array(5) : "&my_array(5)
%>

Output of Example 1.3:

1
2
3
4
5
6
my_array(0) : Bruce Willis
my_array(1) : Josh Hartnett
my_array(2) : Morgan Freeman
my_array(3) : Sir Ben Kingsley
my_array(4) : Lucy Liu
my_array(5) : Stanley Tucci

1.3) Getting Size of Arrays via Ubound() Function

If we want to learn size of an array, we need to use ubound() function. This function returns the number of last element. That means Ubound() function is also based zero.


Example 1.4: Getting Size of Array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%
dim my_array(5)
my_array(0)="Bruce Willis"
my_array(1)="Josh Hartnett"
my_array(2)="Morgan Freeman"
my_array(3)="Sir Ben Kingsley"
my_array(4)="Lucy Liu"
my_array(5)="Stanley Tucci"

Response.Write "my_array(0) : "&my_array(0)&"<br/>"
Response.Write "my_array(1) : "&my_array(1)&"<br/>"
Response.Write "my_array(2) : "&my_array(2)&"<br/>"
Response.Write "my_array(3) : "&my_array(3)&"<br/>"
Response.Write "my_array(4) : "&my_array(4)&"<br/>"
Response.Write "my_array(5) : "&my_array(5)&"<br/><br/>"

Response.Write "ubound(my_array) : "&ubound(my_array)
%>

Output of Example 1.3:

1
2
3
4
5
6
7
8
my_array(0) : Bruce Willis
my_array(1) : Josh Hartnett
my_array(2) : Morgan Freeman
my_array(3) : Sir Ben Kingsley
my_array(4) : Lucy Liu
my_array(5) : Stanley Tucci

ubound(my_array) : 5

Conclusion

We learnt basics of creating arrays. I hope you found this article useful. I am going to explain how to use dimensional arrays next time.

Bookmark and Share
Categories: ASP Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker