[thelist] Looping Psuedocode

Elankath, Tarun (Cognizant) ETarun at blr.cognizant.com
Fri Aug 29 05:47:06 CDT 2003


If they happen to be numbers and they follow each other consecutively, then one can do something like the below. Copy and paste in a html file to test it out.

It conserves space by using the same array. It also conserves time as it approaches quadratic time _only_ in the case of all elements being duplicate elements. For small duplicity it will be efficient.

        <script type="text/javascript">
            var a = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6, 6];
            alert(uniq(a));

            a = [6, 5, 10, 10, 2, 3, 4, 4, 4, 7, 8, 6, 3, 3];

            alert(uniq(a));

            function uniq(list) {
                var i = 0, j = 0;
                while (true) {
                    list[i] = list[j];
                    for (var k = j + 1; k < list.length && list[k] == list[j]; ++k);        
                    if (k == list.length)  {
                        break; 
                    } else {
                        j = k;
                    }
                    ++i;
                }
                list.length = i + 1;
                alert(" i = " + i);
                alert(" j = " + j);
                return list;
            }

            
        </script>

Regards,
Tarun


-----Original Message-----
From: Rob Smith [mailto:rob.smith at THERMON.com]
Sent: Thursday, August 28, 2003 9:03 PM
To: 'thelist at lists.evolt.org'
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

-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: InterScan_Disclaimer.txt
URL: <http://lists.evolt.org/pipermail/thelist/attachments/20030829/294a3307/attachment.txt>


More information about the thelist mailing list