Ok, so iTec asked me to do a tutorial on arrays, which shouldnt take that long really, so its not going to be exactly like the CDONTS one, but i will tidy it up later. 
Arrays are very handy things indeed. Think the like a set of drawers, each "drawer" contains stuff, and when ever you want something out of it, you simply pull the relivant handle. That, to me anyway, is the basic idea behind arrays. So lets have a closer look.
Right, quick note, ASP counts from 0.
Lets make a mini array
Code:
nameArray(0) = "Chris"
There ya go, a nice n' easy array 
lets expand it
Code:
nameArray(0) = "Chris"
nameArray(1) = "David"
nameArray(2) = "Julie"
so now we have three
Easy, huh? 
now lets write them to the browser:
Code:
nameArray(0) = "Chris"
nameArray(1) = "David"
nameArray(2) = "Julie"
response.write(nameArray(0) & chr(13))
response.write(nameArray(1) & chr(13))
response.write(nameArray(2) & chr(13))
And that would write to the browser.
"But wait," you say, "does that mean everytime we want to write the contents of an array, we have to repeat a whole bunch of lines?".
Nope, course not! 
Code:
nameArray(0) = "Chris"
nameArray(1) = "David"
nameArray(2) = "Julie"
I = 0
Do Until I = 3
response.write(nameArray(I) & chr(13))
I = I + 1
Loop
there you go.
But, how would we create an array from a database?! well, like this
Code:
nameArray(0) = RS("Name")
RS.MoveNext
nameArray(1) = RS("Name")
RS.MoveNext
nameArray(2) = RS("Name")
But a quick note, i have a lazy habit of not "Dim"ing my variables. If you do, you would decalre a static array like so
This declares that you are going to have an array with three entries in it.
But what if you where going to take the info out of a database, and you dont know how many entries there is going to be.
What we've discussed so far isnt going ot help, is it?
Well, the array we have been working with is called a static array. Man, we sure have some boring names.
We're now going to look into a (hold you breath) dynamic array. 
Lets take the code we already have, even with the Dim in it
Code:
Dim nameArray(3)
ameArray(0) = RS("Name")
RS.MoveNext
nameArray(1) = RS("Name")
RS.MoveNext
nameArray(2) = RS("Name")
I = 0
Do Until I = 3
response.write(nameArray(I) & chr(13))
I = I + 1
Loop
now, lets rewrite it and make it more dynamic, which, after all, was the point of ASP.
Code:
Dim nameArray()
' Connect to db stuff here
If not RS.EOF then
arrCount = 0
Do Until RS.EOF
ReDim Preserve nameArray(arrCount + 1)
nameArray(arrCount) = RS("name")
arrCount = arrCount + 1
RS.MoveNext
Loop
end if
Set RS = nothing
I = 0
Do Until I = (uBound(arrCount) + 1)
response.write(nameArray(I)
I = I + 1
loop
right, lets go over it then
this is where we decalre out dynamic array. Bear in mind, you don't have to have it.
Code:
' Connect to db stuff here
If not RS.EOF then
now we connect to the database and check to make sure we're not at the end of file.
Code:
arrCount = 0
Do Until RS.EOF
this is our variable that is going to hold our count for us, and we start the loop
Code:
ReDim Preserve nameArray(arrCount + 1)
now we ReDim the array. We have to declare how many entries we're gong to have because we are redimensioning the array, hence the arrCount. The + 1 is there because we need to start counting from 1, but we need to start adding to the array from 0, so this is the easiest way. I think anyway. The Preserve command keeps teh information that is already there.
Code:
nameArray(arrCount) = RS("name")
arrCount = arrCount + 1
RS.MoveNext
Loop
now we insert the databse record into the databse and then increment the arrCount and move the recordset to the next record nad loop.
Code:
end if
Set RS = nothing
I = 0
now we close the conditional statement and the recordset. We also create out count to display the array.
Code:
Do Until I = (uBound(nameArray) - lBound(nameArray) + 1)
response.write(nameArray(I) & chr(13))
I = I + 1
loop
now we start the loop to display the names, just like before. But wait! Whats this uBound and lBound?! Well, these return the indecies of the lowest and highest array elements. So the easiest way to get teh size of an array dynamically is take the difference and add one since you can store a value at both end points. 
Hope your having fun!
now, lets look at other commands
we can easily split a string into an array using... guess... the split() command
, which looks like this
Code:
newArray = split(string, thingToSplitWith)
The newArray is the name of the array we want to create. the string is the information we want to split, and thingToSplitWith is what we use to break up the string.
lets look at an example
Code:
str = "david, chris, julie"
nameArray = split(str, ",")
its that easy, we now have an array that holds the three names, try this code
Code:
str = "david, chris, julie"
nameArray = split(str, ",")
response.write(uBound(nameArray) - lBound(nameArray) + 1 & chr(13)) ' Will return 3
Do Until I = uBound(nameArray) - lBound(nameArray) + 1
response.write("nameArray(" & I & ") = " & nameArray(I) & chr(13))
I = I + 1
Loop
that would return:
Code:
3
david
chris
julie
How easy is this! 
What else is there... i dont really use arrays that much... let me think...
thats all right now... if i can remember other stuff, ill post it later 
** No code has been tested, if you have any problems, please post it here. If i totally ballsed this up, also jus post it here **
Bookmarks