Using Arrays In ASP

Tips >> ASP

 



In these page we have explained where the Arrays are used in ASP and hoe to use arrays in ASP.

What is an Array?

An array is a variable that has multiple 'buckets' associated with it. Instead of creating variables called Book1, Book2, Book3, Book4, Book5, and so on, you simply make one array. Arrays can help you 'group' sets of information together, like the names of states, or names of countries, or colors, or any other grouping. An array is a adjoining space of memory allocated for storing some data. We know when variables are declared, computer's memory is allocated for them.

 

Diagrammatic Representation

 

 

Introduction In Arrays

Example#1 :-

array.asp  
<html>
<title>My Array</title>
<body>

<%
DIM MyData(2,2)
MyData (0,0) = "1"
MyData (0,1) = "2"
MyData (0,2) = "3"
MyData (1,0) = "4"
MyData (1,1) = "5"
MyData (1,2) = "6"
MyData (2,0) = "7"
MyData (2,1) = "8"
MyData (2,2) = "9"

Response.write (MyData (1,2))
%>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Output :-

Line 6: In this example we have defined by using "DIM" an array named Mydata and we have defined the size of the array. We may consider the table bellow as the source of information for our array.

MyData 0 1 2
0 1 2 3
1 4 5 6
2 7 8 9

Lines 7-15 After defining the array we have assigned values to the array.
Line 17 In the response page we will send the value assigned to MyData(1,2) in the array. The first number will be the row and the second the column, so that in our case the response page will show the value "6"

 

How To Work In Arrays

Example#2 :-

In the examples above we have defined all the values within the script one by one, but this assignation may be done in a different way, as it is described in the example bellow:

array3a.asp  
<pre>
<%
MyArray=Array(Zero,one,two,three,four,five,six,seven,eight,nine)
%>

Thearray(0): <% =Thearray(0) %>
Thearray(1): <% =Thearray(1) %>
Thearray(2): <% =Thearray(2) %>
Thearray(3): <% =Thearray(3) %>
Thearray(4): <% =Thearray(4) %>
Thearray(5): <% =Thearray(5) %>
Thearray(6): <% =Thearray(6) %>
Thearray(7): <% =Thearray(7) %>
Thearray(8): <% =Thearray(8) %>
Thearray(9): <% =Thearray(9) %>

</pre>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Output :-

Thearray(0): Zero
Thearray(1): pne
Thearray(2): two
Thearray(3): three
Thearray(4): four
Thearray(5): five
Thearray(6): six
Thearray(7): seven
Thearray(8): eight
Thearray(9): nine

 

Example#3 :-

In this example the array has been create from a string, and each component of the array has been separated by a comma. The Array method will do it for us easily. The use a different string with a different delimiter to define the components in our array is possible.

array3b.asp  

<pre>
<%
TheText="Zero=one=two=three=four=five=six=seven=eight=nine"
Thearray=split (TheText,"=")
%>

Thearray(0): <% =Thearray(0) %>
Thearray(1): <% =Thearray(1) %>
Thearray(2): <% =Thearray(2) %>
Thearray(3): <% =Thearray(3) %>
Thearray(4): <% =Thearray(4) %>
Thearray(5): <% =Thearray(5) %>
Thearray(6): <% =Thearray(6) %>
Thearray(7): <% =Thearray(7) %>
Thearray(8): <% =Thearray(8) %>
Thearray(9): <% =Thearray(9) %>

</pre>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Output :-

Thearray(0): Zero
Thearray(1): pne
Thearray(2): two
Thearray(3): three
Thearray(4): four
Thearray(5): five
Thearray(6): six
Thearray(7): seven
Thearray(8): eight
Thearray(9): nine

Example#4 :-

In this example we have defined the variable TheText, and whithin this variable we have include strings separated by "=". In the next line, we have split the variable TheText into an array of strings (Thearray). Split command have been used to brake TheText and "=" has been used as a delimiter to separate the substrings. In the response page we have indicated the individual values of The array one by one. It may happened to have a variable we want to split, but we do not know how many substrings we may get. In that case we may use ubound command to discover how many elements are in our array, and them we may use that value to write them by using a For-next loop. It can be better explained in the example below :-

array4.asp  
<pre>
<%
TheText="a,f,w,d,u,t,e,u,f,v,o"
Thearray=split (TheText,"=")
%>

How many String do I have in TheArray?
<% =ubound(Thearray)+1 %>

<%
For n=0 to ubound(Thearray)
Response.write (Thearray(n) & "<BR>")
next
%>

</pre>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Output :-

How many Strings do I have in TheArray?
10
a
f
w
d
u
t
e
u
f
v
o

 

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641