[thelist] Looping Psuedocode

Ken Schaefer ken at adOpenStatic.com
Thu Aug 28 20:41:36 CDT 2003


Rob,

I wouldn't call that "elegant" code, nor efficient code. This section:

: while not prodResult.eof
: rcount = rcount + 1
:   redim preserve oldArray(rcount)
: oldArray(rcount) =
: prodResult.fields.item("ProductFamily").value
: prodResult.movenext
: wend

if pretty awful - you don't want to be calling ReDim Preserve inside a loop.
In fact, the ADO Recordset has an internal method that will do this for you:

<%
objRS.Open ...

' DO NOT CALL .MoveFirst HERE. It is completely unnecessary, and may
' result in the query being *re-executed* which you don't want!

If not objRS.EOF then
    arrResults = objRS.GetRows()
End If
%>

which plonks all your data straight into an array, of the correct size, for
you.

Because of the way that you've done the comparison logic, the time that will
be taken for each additional element grows by the number of elements, which
will kill this method as soon as you get any non-trivial amounts of data.
For simplicities sake I would do either of:

a) Use the .getRows() method above to move your data into an array. If you
data is already ordered, then you can loop through the array as many times
are you like in a page without penalty. When you need the distinct list,
just store the previous value in the variable and compare to the current
element to see whether it should be processed

    -or-

b) Run a second SELECT DISTINCT query against your datastore, and store that
in a second array.

Cheers
Ken



----- Original Message ----- 
From: "Rob Smith" <rob.smith at THERMON.com>
To: <thelist at lists.evolt.org>
Sent: Friday, August 29, 2003 1:32 AM
Subject: RE: [thelist] Looping Psuedocode


: Hmm.. I thought this would be a simple.
:
: Ok, these are numbers. I cannot use the DISTINCT because I still need to
: reference my original information of all the "Items" (RecordSet). I just
: need to hide the duplicates or non-unique values until later.
:
: This is what I was going after..
:
: <%
: dim oldArray()
: dim rcount
: rcount = -1
: prodResult.movefirst
:
: while not prodResult.eof
: rcount = rcount + 1
:   redim preserve oldArray(rcount)
: oldArray(rcount) =
: prodResult.fields.item("ProductFamily").value
: prodResult.movenext
: wend
:
: dim newArray()
: eCount = -1
: for each oldElement in oldArray
: eCount = eCount + 1
:   redim preserve newArray(eCount)
:   uniqueElement = true
:   for each newElement in newArray
:   if newElement = oldElement then
:    uniqueElement = false
:    exit for
:   end if
:   next
:   if uniqueElement then
:   newArray(eCount) = oldElement
:   else
:   eCount = eCount - 1
:   end if
: next
:     'for x = 0 to eCount
: ' oldArray (x) =  newArray(x)
: 'next
:
: for x = 0 to ubound(newArray)-1
: if newArray(x) = family then
: response.write("<a href=""get_stock.asp?family=" &
: newArray(x) & """><img src=""grfx/minus.gif"" border=""0"">" & newArray(x)
&
: "</a><br>")
: else
: response.write("<a href=""get_stock.asp?family=" &
: newArray(x) & """><img src=""grfx/plus.gif"" border=""0"">" & newArray(x)
&
: "</a><br>")
: end if
:
: if newArray(x) = family then
: while not prodFamily.eof
: response.write("<a
: href=""inventory_stock.asp?partID=" & prodFamily.fields.item("Part").value
&
: """><img src=""grfx/sub.gif"" border=""0"">" &
: prodFamily.fields.item("Description").value & "</a><br>")
: prodFamily.movenext
: wend
: end if
: next
: %>
:
: and it does what I need it to do which is:
:
: original data:
: Show A Show A Show A
: Show A_1 to Show B   then      Show A_1
: Show A_2 Show C   once Show A_2
: Show A_3    clicked   Show A_3
: Show B    A Show B
: Show B_1 Show C
: Show B_2
: Show B_3
: Show C
: Show C_1
: Show C_2
: Show C_3
:
: So if anyone can use this later, feel free to.
:
: Rob.Smith



More information about the thelist mailing list