custom software development, offshore software development, web enable legacy applications, virtual business, website enhancements  
Home | Success Stories | Services | Partnerships | Contact
         
Custom Web development
elearning
Enterprise Resource Planning Solutions
Customer relationship Management Solutions
Healthcare

Other Development Solutions

 

Let's Talk...
Name
Email
 
  Website Maintenance

ASP arraysBeginner's Guide to Effective use of Arrays in ASP

By Mikhail Esteves

Stylus Systems is a Internet Technology company located in Bangalore India. One of our mottos is "a quality process creates a quality product." Please keep this motto in mind as you learn from this online course. Please contact us if you have any programming needs.

Chapters

  1. Introduction

  2. Defining a simple array

  3. Dynamically re-sizing arrays

  4. Useful array functions

  5. Finding elements in an array

  6. Passing an array to another page

...................................................................................................

Introduction

An array is basically a data structure, which holds a fixed number of variables. Instead of declaring ten string variables to hold ten different elements, you could declare one array and put all the ten elements into the array.

Arrays require the same amount of memory as declaring separate variables, and so they are efficient. To access a particular element in an array, we reference it by its index. The valid index values for an array are the integers 0 to (size - 1). So, if the array has size 10, we can index the elements 0 through 9. Arrays are not complicated structures. Remember that you can use the variables in an array in the same way you use other variables of that type. The same rules apply.

Let us now look into how to define an array, how to dynamically re-size an array, a few useful ASP array functions, how to find elements in an array and also how we can pass an array from page to page.

top
...................................................................................................

Defining a simple array

There are two ways you can define an array in ASP. Lets look at examples for each:

Method 1:

Dim myArray

MyArray=
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct", "Nov","Dec")

Method 2:

Dim myArray(2)

myArray(0)="Jan"
myArray(1)="Feb"

In Method 1, we are defining the entire array in one line. This method is useful for arrays that have a specific use, like the example above that only stores a list of months.

In Method 2, we define values for each individual element on separate lines. This method can be used if you want to store elements from a record set as separate items of an array, or can be used in a loop to define values for each element, for example:

Dim myArray(20)
Dim I

For I=0 to 20
Myarray(I)="This is message " & I
Next

top


...................................................................................................

Dynamically re-sizing arrays

Let us now look at dynamically resizing arrays. Why would you want to do this? Lets for example say you don't know how many items exist in a particular SELECT statement, but you want to store that data in an array. You will not be able to use the above declaration statements unless you get the count of the records and then declare the array. To avoid all that, lets look at the alternative.

ASP comes with a function called REDIM which helps in re-sizing an array. Now, you must be asking, what happens to the data that already might exist in the array if I use the re-dim to resize an array? Well, if you were to use REDIM on its own the previous data in the array would be deleted. This is why the REDIM statement is equipped with another parameter, termed PRESERVE. When this parameter is used, the data in the array will be saved. Lets look at an example:

DIM myArray()
Dim I

REDIM myArray(20) 'This resizes the array to hold 20 items

For I=0 to 20
MyArray(I)="This is item " & I
Next

In the example above, the declaration of the array does not contain the number of items. All it contains is enough to tell ASP that it is an array that's being declared. We then use the REDIM statement to re-size the array to hold 20 items, and fill it in.

Here is another way of writing the same example given above. Note that we use a new function here called Ubound(). Ubound returns the "upper bound" of an array, which only means the number of items already present in the array.

DIM myArray()
Dim I, curRecords

For I=0 to 20
CurRecords=uBound(myArray)
REDIM PRESERVE myArray(CurRecords+1)
MyArray(I)="This is Item " & I
Next

What we do in the for statement here is, get the number of elements in the array, use the REDIM PRESERVE function to resize the array to the number of elements plus one. The Preserve statement is essential here as we want to preserve the data that already exists.

top
...................................................................................................

Useful array functions

Function: Ubound(arrayName)

This function returns the "upper bound" index of an array, which basically is the number of elements in an array.

Dim myArray(10)
Dim I

For I=0 to 10
MyArray(I)="Item " & I
Next
Response.write "ubound(myArray) = " & ubound(myArray)

Function: Lbound(arrayName)

This function returns the "lower bound" index of an array, that basically is the first element in an array. Use the same example above to test the Lbound() function (remember to change the function ubound to lbound though :)

Function: Split(string, splitby)

This function helps in converting a string into an array. Lets see an example using this function:

Dim myString
Dim myArray
Dim I

MyString = "a,b,c,d"
MyArray = split(MyString,",")

For I=0 to Ubound(MyArray)
Response.write MyArray(I) & "<br>"
Next

All we did was put up a string separated by comma's. We then used the split command to split the string using "," and store that into an array. Next we ran a for loop to display the results of an array, similar to the other examples at the beginning of this tutorial.

top
...................................................................................................

Finding elements in an array

Here is a function that would return the "index" of the element in an array. The index is basically the position of the element, for example, 0 which stands for myArray(0).

function findArray(arrName,srcStr)
dim I
dim pint,cnt

cnt=0

for i=0 to ubound(arrName)
if cnt=0 then
if srcStr=arrName(i) then
pint=i
cnt=1
end if
end if
next
findArray=pint+1
end function

Lets now see an example that uses the example above:

Dim myArray(20)
Dim k

For k=0 to 20
MyArray(k)="Item " & k
Next

Response.write findArray(myArray,"Item 8") & "<p>"
Response.write myArray(findArray(myArray,"Item 8"))

The second line directly gets the index of the element, passes it to the array, returning the value from the array.

top
...................................................................................................

Passing an array to another page

There are various ways you could pass an array to another page. Lets see the three possibilities:

  • Create the entire array into a string separated by comma's, and use the split() function on the next page to re-create the array
  • Store the array in a session variable, and access it on the next page.
  • Pass the array using hidden fields of a form, which automatically come comma-separated, and create the array using the split() function.

The first two are good, but are relatively more complex than the third way. Let us look at the 3rd possibility only because it's the simplest and most effective.

Page1.asp

<%
dim I
dim myArray(20)

for I=0 to 20
myArray(I)="Item " & I
next
%>
<html>
<body>
<form name="testform" method="post" action="page2.asp">
<%
for I=0 to ubound(myArray)
response.write "<input type=hidden name=myArray value='" & myArray(I) &
"'>"
next
%>
<p>
<input type="submit">
</form>
</body>
</html>

All we are doing is storing each individual element of the array as separate hidden fields in the form. Lets now see how to get them out on page2 and display them:


Page2.asp

<html>
<body>
<%
dim arrString
dim myArray
dim I

arrString=request("myArray")
myArray = split(arrString,",")

for I=0 to ubound(myArray)
response.write "Item "&I&" = " & myArray(I) & "<br>" & vbCrLf
next
%>
</body>
</html>

We have now covered the basics and a few advanced topics of arrays in ASP. Here are a few links that could help you further in the use of arrays:

top

The place asp developers GO! Aspin.com Webmaster Tools Central ASP Help

...................................................................................................

Website Personalization

Related Links on Expanding your Site's Potential:

 

 
  Case Studies

About Stylus | India Advantage | Site Map
Archives. Copyright 2003 Stylusinc.com. All Rights Reserved. Privacy Policy

Clear about what you want? Click Here

FAQs: Offshore Development | Custom Software Development