+91-943-185-6038 me@shashidharkumar.com


Introduction
Working with Arrays
Filtering values from a array
Creating a table from data in a string
Simple keyword search

Introduction

Instead of having our information (variables or numbers) in variables like
Mydata1, Mydata2, Mydata3 etc, by using arrays our information will be in an
unique variable. Let´s check an example:

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
22
23
24
25
26
27
28
29
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.<br>

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 16. 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”

Top
Very often, we will defined an array from data obtained from a table with only one column. Let´s check an example:

array2.asp
<html>
<title>My Array</title>
<body><%
DIM MyData(9)
MyData (0) = “0”
MyData (1) = “1”
MyData (2) = “2”
MyData (3) = “3”
MyData (4) = “4”
MyData (5) = “5”
MyData (6) = “6”
MyData (7) = “7”
MyData (8) = “8”
MyData (9) = “9”
Response.write (MyData (5))
%></body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Original table for the array in the script

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

In the response page we will send the value assigned to MyData(5) in the array. The response page will return 5.<br>

It is also possible to define an array with more dimensions as for example MyData(5,5,5,5).
Top

Working with Arrays

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:
<br>

array3a.asp Resulting page
<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>
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

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.
We may also want to use a different string with a different delimiter to define the components in our array:

array3b.asp Resulting page
<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>
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

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 Thearray one by one.

It may happend 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 (see example below).

array4.asp Resulting page
<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>
How many Strings do I have in TheArray?
10a
f
w
d
u
t
e
u
f
v
o

Top
Filtering values from a array

In the next example we will filter the information in our array, and we will display only part of it.

array5.asp
<pre>
<%
dim MyArray(9)
MyArray (0) = “Zero”
MyArray (1) = “One”
MyArray (2) = “Two”
MyArray (3) = “Three”
MyArray (4) = “Four”
MyArray (5) = “Five”
MyArray (6) = “Six”
MyArray (7) = “Seven”
MyArray (8) = “Eight”
MyArray (9) = “Nine”
%>
Find strings containing “t” (case sensitive)
<% =join(filter(MyArray,”t”,True,0),”,”) %>
Find strings containing “t”
<% =join(filter(MyArray,”t”,True,1),”,”) %>
Find strings which do not contain “t” (case sensitive)
<% =join(filter(MyArray,”t”,False,0),”,”) %>
Find strings which do not contain “t”
<% =join(filter(MyArray,”t”,False,1),”,”) %></pre>
Find strings containing “t” (case sensitive)
Eight
Find strings containing “t”
Two,Three,Eight
Find strings which do not contain “t” (case sensitive)
Zero,One,Two,Three,Four,Five,Six,Seven,Nine
Find strings which do not contain “t”
Zero,One,Four,Five,Six,Seven,Nine

The array and the assignation of values has been done as usually, and in the second part of the script we have used some lines similar to this one:
<% =join(filter(MyArray,”t”,True,0),”,”) %>

In this lines we have filter the values at MyArray and we have join them.

filter(MyArray,”t”,True,0)
This part of the line have search for “t” in MyArray.
True means we have selected the strings containing the search string (in this case “t”).
False will indicate we are selecting the strings which do not content the search string.
0 means our search is case sensitive (a binary comparation)
1 will mean it is not a case sensitive search (a textual comparation)
join(filter(MyArray,”t”,True,0),”,”)
The complete line will join the filtered strings with the delimiter indicated (in this case “,”)

Top
Creating a table from data in a string

In order to undertand this script we will consider we have a table like the one bellow, and that this table was the original source of information we used to create our table:

Peter student Chicago 123
John teacher London 234
Sue Manager Sidney 789
From the table we got this three lines by separeting the values by commas:Peter,student,Chicago,123
John,teacher,London,234
Sue,Manager,Sidney,789

And finaly we conected the three lines by

separeting the values with “/”:

Peter,student,Chicago,123/

John,teacher,London,234/

Sue,Manager,Sidney,789

The string obtained was saved to a variable named Mydata in the script bellow. The resulting page will show a table like the original. This script is not limited by number of rows or columns (the maximun amount of then is calculate each time we run the script).

Createatable.asp
<%
Mydata=”Peter,student,Chicago,123/John,teacher,London,234/Sue,Manager,Sidney,789″
Createtable()
%><%¼br> Sub CreateTable()MyRows=split (Mydata,”/”)¼br> RowsNumber=ubound(MyRows)Response.write (“<table border=1>”)
For i=0 to RowsNumber
DatainRow=split (MyRows(i),”,”)¼br> NumberofDatainRow=ubound(DatainRow)
Response.write (“<tr>”)
For n=0 to NumberofDatainRow
Response.write(“<td>” & DatainRow(n) & “</td>”)
Next
Response.write (“</tr>”)
Next
Response.write (“</table>”)End Sub
%>

This script may be used for several porpouses: we may generate Mydata by filtering values from an array as shown bellow:

<%
Dim Myclients(3)
Myclients(0)=”Peter Smith,Chicago,Manager,123″
Myclients(1)=”John Smith,New York,Accountant,124″
Myclients(2)=”George Smith,Chicago,Administration,245″
Myclients(3)=”Sam Smith,Dallas,Consultant,567″SearchFor=”Chicago”

Mydata=join(filter(Myclients,SearchFor,True,1),”/”)

Createtable()
%>

This code in combination with Createtable() Subroutine in the previus example will display only the clients from Chicago. The SearchFor variable may be obtained from a form.
<br>
Top
Simple keyword search

In this example, in our first visit a form asking for a keyword will be display. After submitting the keyword Toredirect() Subroutine will be activated.

In this Subroutine we have create two arrays: Myinfo has a description of the URL located at MyURL. In case the keyword is included in the description of the site, the visitor will be redirected to the corresponding URL. Both arrays may be very very long.
<br>

search.asp
<% if request.form=”” them %>
<form method=post action=search.asp>
<input type=text name=keyword>
<input type=Submit value=Search>
</form>
<%<br> else
Toredirect()
end if<br> %><%
Sub Toredirect()dim Myinfo(4)
Myinfo (0) = “Asp tutorial for beginners”
Myinfo (1) = “Displaying Date Time and Text”
Myinfo (2) = “Using Variables and Forms”
Myinfo (3) = “If…Then and For…Next instructions”
Myinfo (4) = “Do…Loop and Select…Case instructions”dim MyURL(4)
MyURL (0) = “http://www.shashidharkumar.com”
MyURL (1) = “http://www.shashidharkumar.com/Datetime.htm”
MyURL (2) = “http://www.shashidharkumar.com/Forms.htm”
MyURL (3) = “http://www.shashidharkumar.com/if_then-for_next.htm”
MyURL (4) = “http://www.shashidharkumar.com/Do_loop-Select_case.htm”Numberofpairs=ubound(Myinfo)
For n=0 to Numberofpairs
if inStr(Myinfo (n), request.form (“keyword”))>0 then
Response.redirect(MyURL(n))
end if
NextResponse.write (“The keyword has not been found”)
End Sub
%>
See also  ASP - Date,Time and Text