Total members 11797 |It is currently Sun Sep 22, 2019 12:28 am 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
(3Declaring 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
(strArrayTarget, 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
(3Declaring 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





Author:
Mastermind
User avatar Posts: 2707
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     -  
 Multidimensional arrays in Java     -  
 How to compare two arrays in java     -  
 Merge two or more arrays recursively     -  
 Computes the intersection of arrays     -  
 JSP Passing Arrays to Methods     -  



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