Total members 11895 |It is currently Mon Dec 30, 2024 7:04 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In this article we are going to show how declare an array in ASP, to define an array you have to define its size and variable name:
Code:
Dim arrColors(2)

To fill the elements of this array you will use call by index like this:
Code:


arrColors 
(0) = "Red"
arrColors (1)="Green" 
arrColors 
(2)="Blue"
 

You may notice that in the array declaration step we set 2 between the array braces, while in the array filling we have three elements, the point is 2 is equals to the maximum index and not to size, for example : arrNew(3) means size is equal 4. A question may come to the mind, what will happen if we set larger than the limit? The answer that you will error message telling you that you have declared arrColors with fixed number X while you are trying to index at Y. The solution to this problem is using dynamic arrays instead of fixed ones. To declare dynamic array you just follow the example below:
Code:
<% 
    X 
= 15 
    Dim arrColors 
() 
    ReDim arrColors 
(X) 
%>
 

Creating a multi-dimensional array
Code:
<% 
    X 
= 15 
    Y 
= 10 
    Dim arrColors 
() 
    ReDim arrColors 
(X,Y) 
%>
 

Now we control the size of the array dynamically with a variable, on your running code you can change the array size using a variable, another case, is when you want to increase the size of array while keeping the existing array values if exists, you will use the Preserve keyword :

Code:
<%
arrColors (0) = "Red"
arrColors (1)="Green"
Keep Added two elements and re-declare and add two more elements
ReDim Preserve arrColors 
(3)
arrColors (2)="Blue"
arrColors (3)="Yellow"
%>
 


Using Preserve keyword in Loop,
Code:
<% 
    Dim arrColors 
() 
    for i 
= 1 to 10 
        ReDim Preserve arrColors 
(i) 
       arrColors
(i) = "Color" & i 
    next 
%> 


To print the content of array use:
Code:
<%
For Each myColor In arrColors
Response
.Write(myColor  & "<br>")
Next
%>
 


To get the current length of an array you can use the function UBound(), as a index starts at 0 and if there 7 elements in the array the UBound function will return 8, so the size is equals UBound Return plus one :
Code:

Dim arrColors
(3)  Declaring an array

arrColors
(0) = "Red"
arrColors(1)="Yellow"
arrColors(2)="Green"
arrColors(3)="Blue"
Response.Write ("UBound return is = " & UBound(arrColors) )

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


One of important functionality exists in ASP(VB) arrays is splitting, here we split strings using the function split(), here we split strings using a delimiter that is can be space, comma , or any character you define.
Code:

mArray
=split("Name is samy"," ")  Space is a delimiter

and this is equals to
Code:

mArray
(0)="Name"
mArray(1)="is"
mArray(2)="samy"
 


Note that you also define the maximum splits used the third parameter in the split function like this :
Code:

mArray
=split("Name is samy and i am a developer"," ",3)
 

and this is equals to :
Code:

mArray
(0)="Name"
mArray(1)="is"
mArray(2)="samy and i am a developer"
 


as we split the strings we can also concatenate strings using the join() function, see the example below :
Code:

Dim myColors
(3) Declaring an array

myColors
(0) = "Red"
myColors(1)="Yellow"
myColors(2)="Green"
myColors(3)="Blue"

For Each myColor In myColors
Response
.Write(myColor  & "<br>")
Next

myStr
=join(myColors,",")
Response.Write(myStr & "<br>")
 


Last function is also a ASP(VP) function which is filter() function , this function used for strings search, the function prototype is as follows :
Code:

Filter
(strArray, Target, include, comparetype)
 

strArray is the array where will be searching in , Target is the string looking for . include, comparetype parameters are optional. include can be True or False. If it is True then all matching elements of the strArray is returned The parameter comparetype is also a Boolean , if it is set to 0 then it is a binary comparison so it is case sensitive , 1 ignores case sensitive.
Code:

Dim myColors
(3) Declaring an array

myColors
(0) = "Red"
myColors(1)="Yellow"
myColors(2)="Green"
myColors(3)="Blue"

For Each myColor In myColors
Response
.Write(myColor & "<br>")
Next
Targetstring
="Green"
filterResults=Filter(myColors,Targetstring)


Response.Write "<br/> Search Results <br/>"
For Each filterItem In filterResults
Response
.Write(filterItem & "<br>")
Next



This is an ASP (Active Server Pages) code snippet that declares an array named "myColors" and assigns values to its elements. The array is declared with 4 elements, indexed from 0 to 3. The values assigned to the elements are "Red", "Yellow", "Green", and "Blue". The For Each loop iterates through the elements of the array and writes their values to the response using the Response.Write method. Then it declares a variable named "Targetstring" and assigns the value "Green" to it. The code then uses the built-in Filter function in ASP to filter the array "myColors" based on the value of "Targetstring" and assigns the filtered results to a variable "filterResults". Finally, another For Each loop is used to iterate through the filtered results and write their values to the response using the Response.Write method. The results will be the elements of the array that have the value of "Green". This code demonstrates basic array manipulation and the usage of built-in functions in ASP.


This code also demonstrates the basic usage of For Each loop in ASP. The For Each loop allows you to iterate through a collection of items, such as an array, without knowing the number of items or the index of each item. It is useful for iterating through the elements of an array, or the items in a dictionary or other collection. The Response.Write method is used to write output to the web page. It is a built-in function in ASP that sends text or other data back to the browser. This code, it is used to write the values of the elements in the array and the filtered results to the web page. The <br> is used to include a line break in the output, this way, the output will be displayed in multiple lines.


In addition, it also demonstrates the usage of the string concatenation operator "&" in ASP. In this code, the "&" operator is used to concatenate the value of the current element in the loop with the string "<br>" to add a line break after each element. This code snippet also shows how to filter an array based on a certain value and iterate through the filtered results. It also shows the usage of the built-in function "Filter" in ASP, The filter function is used to filter the elements of an array based on a certain condition, it takes two arguments, first the array and the second one is the value to be searched for. Also, it shows the basic usage of variables in ASP, it declares and assigns values to variables, and uses them in the code.



There are a few other things that can be noted about this code snippet. Firstly, it uses the "Next" statement at the end of the For Each loop, which tells the interpreter to move to the next element in the array, this is how the loop iterates through the elements. Also, it demonstrates the usage of line breaks <br> in the output, this way, the output will be displayed in multiple lines, making it more readable. Additionally, this code snippet is server-side code, which means that it runs on the server and generates HTML or other output that is sent to the browser. This allows for dynamic behavior on the webpage since the output can change based on user input or other conditions.

In summary, this code demonstrates basic array manipulation, usage of built-in functions, for each loop, string concatenation, outputting to a web page, variables, filter, line break, and server-side scripting.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Defining arrays in ASP
 Arrays in photoshop     -  
 Concept of arrays     -  
 Multidimensional Arrays in JSP     -  
 Arrays in java     -  
 Arrays using Pointers     -  
 Variable Size Arrays     -  
 Multidimensional arrays in Java     -  
 How to compare two arrays in java     -  
 Merge two or more arrays recursively     -  
 Computes the intersection of arrays     -  



Topic Tags

ASP Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com