Append to an array??

Results 1 to 2 of 2

Thread: Append to an array??

  1. #1
    Join Date
    Dec 1969

    Default Append to an array??

    I need an array type object that i want to place two fields from a recordset into but only if they match specified criteria. What I&#039m doing is, looping thru a recordset and if the fields match the criteria then place both into an array, if no match don&#039t.<BR><BR>So I don&#039t know how the size of the array at start, is there a way to append to the array? I won&#039t know the size until the loop is complete. <BR><BR>I tried a dictionary object because of the ".Add" but was getting "This key is already associated with an element of this collection" when I know the key (an IP address) for existing item (a string) in the dictionary object was not the same as one trying to be entered. Also is there a way to display everything is a dictionary object?<BR><BR>I hope this isn&#039t too confusing, in a bit of a hurry to get this out on the message board. Thanks for any help.

  2. #2
    Karl Guest

    Default RE: Append to an array??

    You can dynamically resize arrays..I haven&#039t done it much, but the following code should work.<BR><BR>when you declare your array at first use redim instead of dim:<BR>&#039put whatever number you want for your initial size<BR>redim myArray(10) <BR><BR><BR>then as you are looping through your recordset:<BR><BR>redim myArray(10)<BR>dim counter = 1 &#039array indexer<BR>do while not rs.eof<BR> if (this is the record I want to put in an array) then<BR> if counter+1 = UBOUND(myArray) then &#039if we are @ max size<BR> redim PRESERVE myArray(counter + 1)<BR> myArray(counter) = rs("whatever")<BR> else<BR> myArray(counter) = rs("whatever")<BR> end if<BR> end if<BR> rs.movenext<BR> counter = counter + 1<BR>LOOP<BR><BR>there are a lot of ways to do what I just did. Here&#039s a line by line algorithm of what&#039s happening in case you are lost<BR><BR>declare array<BR>DECLARE and ASSIGN a counter to keep track of our array index<BR>LOOP until end of recordset is reached<BR> IF row matches criteria<BR> IF the counter+1 has reached the upper-bound of the array<BR> redefine the array to it&#039s currentsize + 1<BR> store the recordset value into the array<BR> else (if we haven&#039t reached the upper limit)<BR> store the recordset value into the array<BR> END IF<BR> END IF<BR> MOVE to the next record<BR> INCREMENT counter<BR>END LOOP<BR><BR>If you don&#039t use the keyword "preserve" when redimensionalizing your array, all the data will be lost.<BR><BR><BR>as for listing everything in a directionary object, try:<BR>for each thing in MyDict<BR> response.write MyDict.Item(thing)<BR>next<BR><BR>it *SHOULD* work...<BR><BR><BR>don&#039t know what the problem with the "key already associated.." is..dont&#039 use the dictionary object at all.<BR><BR><BR>On a side note, you might find that using the rs.getrows might also do the trick..although it&#039s a little more complicated seeing as it&#039s a 2 dimensional array...if you are interested..<BR><BR>--<BR>Karl

Posting Permissions

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