Free Guides

Language Tutorials


           

ASP ( Active Server Pages )

                 Index              

Your Ad Here

 

Declaring array in ASP

We have seen how to declare variables in VBScript used in ASP. Now we will try to learn how we can declare and use arrays inside ASP code. Let us try using declare one array with fixed number of elements.

Dim myArray(1)

The above line declared a fixed width array. Now we will try to add element to this array.

myArray(0) = "UK"
myArray(1)="USA"

You can see we have added two elements to this array. As the array starts with 0 element so we can add 2 elements if it is declared like this. Same way myArray(5) can store 6 elements , starting from 0 element.

What happens if we try to add one more element to the above array? We will get error message as we declared the myArray element as fixed number array. To change the number of elements to be stored , we have to declare the array as dynamic size array. But before adding the element we must re-declare the size of the array. Here is the code to declare a dynamic size array.
 

Dim myArray() 'Declaring a dynamic array
ReDim myArray(1) 'Re Declaring a dynamic array
myArray(0) = "UK"
myArray(1)="USA"

 

You can see we have re-declared the array with size and then stored elements inside it. Now as we declared this element at the starting as a dynamic array we can re-declare this array with any size. If we are not declaring as dynamic array then we can't change the size by re-declaring the array. The code below will generate error.

 

Dim myArray(1) 'Declaring a fixed array
ReDim myArray(2) 'Re Declaring the fixed array
 




Now let us work with adding more elements to an dynamic array. Here we will first declare a dynamic array and then add two elements to it by re-declaring the size of the array. Then we will try to add two more elements to the same array by preserving the old elements. To add more elements we have to use preserve while re-declaring the array like below.

 

Dim myArray() 'Declaring a dynamic array
ReDim myArray(1) 'Re-Declaring a dynamic array

myArray(0) = "UK"
myArray(1)="USA"
'Added two elements now let us redeclar and add to more elements
ReDim Preserve myArray(3)
myArray(2)="Canada"
myArray(3)="UAE"


For Each item In myArray
Response.Write(item & "<br>")
Next

 


We have included the for each loop to display all the element of the array. The output will display all four elements we added now.

Length of an array in ASP by using UBound function

We can get the length or total number of element in the array by using UBound() function. Note that the index of the first element of the array is 0. So if there are five elements inside an array then the function UBound() will return 4. Here is the code to get the highest index of the array. Note that total element is 1 plus the highest index.

 

Dim myArray(3) 'Re-Declaring a dynamic array


myArray(0) = "UK"
myArray(1)="USA"
myArray(2)="Canada"
myArray(3)="UAE"
Response.Write ("Total element of the array = " & UBound(myArray) )

for i=0 to uBound(myArray)
Response.Write "<br>" & myArray(i)
Next

 

Here UBound is used to set the maximum looping length to display each element of the array.
 

Creating arrays by breaking strings using split function

We can break a string and create an array by using split() function in ASP ( VBScript). We can decide what way the array to be created by breaking the string. If we break the string by using one space as a delimiter then the array will contain all words of the string. Same way we can take any other delimiter like coma ( , ) or any other character and generate an array by breaking the string. Here is the syntax of split function.

My_Array=split(My_String," ")


 

Here My_String is the string variable and we are using one space length as delimiter and breaking the string. We will get our array in the variable My_Array.

We can restrict the total elements of the array by specifying another options parameter to split function. This way the last element will contain rest of the string. This example will make it simple.

 

My_String="Welcome to plus2net, learn web programming and design"
My_Array=split(My_String," ",3)



Here is the value of each item of the array we get after the split function

 

My_Array[0] = Welcome
My_Array[1] = to
My_Array[2] = plus2net, learn web programming and de
sign


 

You can see the last element contains all the words of the string.

Here is the full code
                             

Dim My_String
Dim My_Array
My_String="Welcome to plus2net, learn web programming and design"

My_Array=split(My_String," ")
For Each item In My_Array
Response.Write("<br>" & item)
Next
 

Same way we can create one string by joining all elements of an array by using Join() function.
 

Join function to add all elements of an array to create string variable in ASP

We can create a string by joining all the elements of an array by using join() function. The out put of this function is a string variable. Here is the syntax of using join() function.

 

My_String = join(My_Array)

Here My_Array is the array and the My_String is the string variable which gets the string which is returned by join function after adding all the elements of the My_Array array.

While joining by default one blank space is used as delimiter, we can specify any delimiter while joining the elements by using join function. Here is the syntax of this.
 

my_String=join(myArray,",")

This way we can have any delimiter added between the elements while generating the string variable. In the above example comma ( , ) is used.

Here is the complete code for joining elements and creating a string variable.
 

Dim myArray(3) 'Declaring a dynamic array

myArray(0) = "UK"
myArray(1)="USA"
myArray(2)="Canada"
myArray(3)="UAE"

For Each item In myArray
Response.Write(item & "<br>")
Next

my_String=join(myArray,",")
Response.Write(my_String & "<br>")

Searching inside an array for matching elements using Filter function.

We can search inside an array for a string using Filter() built in function of VB used in ASP. The search or find operation will return another array with the result. We can use different arguments to match our requirement while using Filter function. This function works like different types of string functions like InStr, Replace etc. Here is the syntax of Filter() function.

 

Filter(my_array, search_string, include, comparetype)


Here my_array is our main array inside which we will be searching, search_string is the string variable or the string itself to be searched inside my_array. The other two arguments include and comparetype are optional arguments. include is a Boolean value, it can be True or False. If it is True then all matching elements of the my_array is returned and if it is set to False then all not matching elements are returned. The argument comparetype can take value of 0 or 1, if it is set to 0 then it is a binary comparison so it is case sensitive as a result lower and upper case letters are treated as different. If the value of comparetype is set to 1 then it will be a text comparison so it is case insensitive, as a result lower case and upper case text are treated as same.

Let us try with this simple code on how to use Filter function

Dim myArray(3) 'Declaring a dynamic array
myArray(0) = "UK"
myArray(1)="USA"
myArray(2)="Canada"
myArray(3)="UAE"

For Each item In myArray
Response.Write(item & "<br>")
Next
search_string="UA"
my_filter=Filter(myArray,search_string)


Response.Write "<br>-----After Filter------<br>"
For Each item In my_filter
Response.Write(item & "<br>")
Next
 



The output of the above code is here

 

UK
USA
Canada
UAE
-----After Filter------
UAE

By default the argument include is set to True and comparetype is set to 0. So if we search for lower case word ua ( in place of UA ), we will get empty result. We can add the value of these two optional arguments and see the result. The Filter function can be modified like this
 

my_filter=Filter(myArray,search_string,True,1)

You will get different results by making include = False and changing comparetype to 0.