SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Quick Array tutorial for iTec

  1. #1
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Array tutorial for iTec

    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

    Code:
    Dim nameArray(3)
    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

    Code:
    Dim nameArray()
    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 **

  2. #2
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D, not to rain on your parade, but your code /could/ be a little more efficient. I will explain the how/why/when about the samples. For your last example, you had this code block:

    Code:
    '   dhtmlgod's 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
    First: Why create a string of names, forcing the server to allocate space for the variable and data, then create an array with that string, forcing the server to allocate space again? Sometimes, this would be a good method, possibly when you are concatenating the list in a loop, etc. In this case, it is inefficient to use them back to back.
    Second: It is a good idea to avoid using Do While or Do Until loops as much as possible. A simple typing mistake in the code and the server could go into an endless loop. In the case of an array, there is a built in iterator, that is, built in looping mechanism, the index. This makes it possible to use For Idx = .. To ... and For Each In loops, which have a far less chance of becoming an endless loop, etc. Here are 2 samples using these methods.

    Code:
    '	KodeKrash's Kode - arrSample(0)
    nameArray = Array( "david", "chris", "julie" )
    Response.Write( "There are " & UBound( nameArray ) - LBound( nameArray ) + 1 & " names in the list." )
    For Idx = LBound( nameArray ) To UBound( nameArray )
    	Response.Write( "Name :" & Idx & " = " & nameArray(Idx) & "<br>" )
    Next
    
    '	KodeKrash's Kode - arrSample(1)
    nameArray = Array( "david", "chris", "julie" )
    Response.Write( "There are " & UBound( nameArray ) - LBound( nameArray ) + 1 & " names in the list." )
    For Each Item In nameArray
    	Response.Write( Item & "<br>" )
    Next
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

  3. #3
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D, man, I seem to have it out for you today... )

    Regarding using Arrays with RecordSets:

    I can think of VERY few times when a developer would need to put the results of a RecordSet in an Array. This is basically a redundant functionality, since RecordSets are collections, which are very similar to Arrays in many ways. You can use For Each loops (similar to my easlier post's examples) to get all the data from a RecordSet, plus many many other things.

    Maybe one day I will write a comprehensive ADODB/ ADOX/DAO tutorial, but until then, samples will have to wait.
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

  4. #4
    SitePoint Wizard big_al's Avatar
    Join Date
    May 2000
    Location
    Victoria, Australia
    Posts
    1,661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by KodeKrash

    Regarding using Arrays with RecordSets:

    I can think of VERY few times when a developer would need to put the results of a RecordSet in an Array. This is basically a redundant functionality, since RecordSets are collections, which are very similar to Arrays in many ways. You can use For Each loops (similar to my easlier post's examples) to get all the data from a RecordSet, plus many many other things.
    Hate to rain on your parade now Kode.

    But If you use GetRows your script becomes alot more efficient, because the Recordset is open for less time.

    D, Very nice tutorial. I know arrays was the most daunting thing I experienced while I was learning ASP
    .NET Code Monkey

  5. #5
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    big_al:

    Good point about GetRows, as far as data retrieval in a static sense. Most of my ASP work involves fuzzy logic and often artificial intelligence, and GetRows is not very flexible in a highly abstract deployment. For most uses, you are correct in your comparision of GetRows and RecordSets.
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

  6. #6
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm...

    ok, so the tutorial was crap

    as i said, i never really use arrays very much, but thanks for the tips

  7. #7
    SitePoint Wizard big_al's Avatar
    Join Date
    May 2000
    Location
    Victoria, Australia
    Posts
    1,661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by dhtmlgod
    hmm...

    ok, so the tutorial was crap

    as i said, i never really use arrays very much, but thanks for the tips
    I wouldn't say that. I think Kode just wanted to point out some other methods. As we all know there are no right and wrongs, just different ways of doing things
    .NET Code Monkey

  8. #8
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, D.. There is no right or wrong answer, but darn it, if there was a wrong answer what was probably it. LOL, my literature teacher from high school used to say that, couldn't resist the opportunity.
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •