Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distinct values in an array 1

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
I am calling a recordset using getstring to create an array. I am doing this because some of the records ate single dates, multiple dates seperated by commas, and dates with text strings seperated by commas. I use a loop to weed out the non-dates using isDate. I then put all the results in a select box for a form.

Is there a way to only write distinct values? I'm not sure how to refer back into the collection. If I was doing it in the db I could use IN.

Here is a quick mockup

<%@LANGUAGE=&quot;VBSCRIPT&quot;%>
<!--#include file=&quot;Connections/connABS_IT.asp&quot; -->
<%
Dim rs
Dim rs_numRows

Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.ActiveConnection = MM_connABS_IT_STRING
rs.Source = &quot;SELECT i_raised_by FROM tblIssues&quot;
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 1
rs.Open()

rs_numRows = 0
%>
<%
TM = rs.GetString(,,,&quot;,&quot;)
%>
<%
rs.Close()
Set rs = Nothing
%>
<%
Dim TM

tmarray = split(TM,&quot;,&quot;)
response.write &quot;<select name='seminars'>&quot;
for i = 0 to UBOUND(tmarray)
tmitem = cStr(tmarray(i))
IF isDate(tmitem) = true then
response.write &quot;<option>&quot; & tmitem & &quot;</option>&quot;
end if
next
response.write &quot;</select>&quot;
%>

 
I would use Dictionary to check for duplicated values. Before you add one item (date) into the Dictionary, you use object.Exists(key) to check.

If (object.Exists(key) Then
'do nothing
Else
'add into Dictionary
End If
 
Okay, I'm a bit confused. Here is the code I came up with:

TM = rs.GetString(,,,&quot;,&quot;)
tmarray = split(TM,&quot;,&quot;)
set d = CreateObject(&quot;Scripting.Dictionary&quot;)
for i = 0 to UBOUND(tmarray)
tmitem = cStr(tmarray(i))
'tmitem = trim(tmitem)
IF isDate(tmitem) = true then
IF (d.Exists(tmitem)) THEN
'do nothing
ELSE
d.Add tmitem, tmitem
END IF
end if
next
keys = d.keys
For each x in keys
response.write d.item(x)
next

----------------------------

Here are the results:
Oct. 22 Nov. 3Oct 22Oct. 23Oct. 27 Sept 23 Oct. 22 Oct. 27 Sept 26 Sept. 26Oct. 2Oct. 3Oct. 8 Oct. 23 Oct. 31Oct. 31 Nov. 10Nov. 3Nov. 5Nov. 10

I still get duplicates. However, if I un comment the TRIM function above and re-run the code, i get:

Oct. 22Nov. 3Oct 22Oct. 23Oct. 27Sept 23Sept 26Sept. 26Oct. 2Oct. 3Oct. 8Oct. 31Nov. 10Nov. 5

Why would I get different results?
 
By the bye, the duplicates are caused by the absence of a period in some of the array items. I treated it with

tmitem = replace(tmitem, &quot;.&quot;, &quot;&quot;)
tmitem = replace(tmitem, &quot; &quot;, &quot;. &quot;)
 
Here is the code with the original striung, if anyone can help:

I get different result when I take out the trim function.

'TM = rs.GetString(,,,&quot;,&quot;)
TM = &quot;Oct. 22, Nov. 3,Oct. 22,Oct. 22,Oct 22, Nov.3,Oct. 22,Oct. 22,Oct. 22,Oct. 23,Oct. 27,Oct. 27,Oct. 27,Oct. 27,Oct. 27,Oct. 27,,Oct. 27,Pilot 1, Sept 23, Pilot 2, Sept.26, Oct.8, Oct. 22, Oct. 27,,Pilot 2, Sept 26,Pilot 2, Sept. 26,Pilot 2, Sept. 26, Oct. 22,Oct. 2,Oct. 2,Oct. 2,Oct. 3,Oct. 3,Oct. 8,Oct. 8,Oct . 8,Oct. 8, Oct. 23,Oct. 27, Oct. 31,Oct. 27,,Oct. 27,Oct. 27,Oct. 27,Oct. 31,Oct. 31,Oct. 31, Nov. 10,Nov. 3,Nov. 3,Nov. 3,Nov. 3,Nov. 3,Nov. 3,Nov. 3,Nov. 3,Nov. 3,,Nov. 3,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5, Nov. 10,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 5,Nov. 10,Nov. 10,,,&quot;
'rs.Close()
'Set rs = Nothing
%>
<%
Dim TM
tmarray = split(TM,&quot;,&quot;)
set d = CreateObject(&quot;Scripting.Dictionary&quot;)

for i = 0 to UBOUND(tmarray)
tmitem = cStr(tmarray(i))
tmitem = trim(tmitem)
tmitem = replace(tmitem, &quot;.&quot;, &quot;&quot;)
tmitem = replace(tmitem, &quot; &quot;, &quot;. &quot;)
IF isDate(tmitem) = true then
IF (d.Exists(tmitem)) THEN
'do nothing
ELSE
d.Add tmitem, tmitem
END IF
end if

next
keys = d.keys
For each x in keys
response.write d.item(x)
response.write(&quot;<br />&quot;)
next
 
if your datatype for datetime is char then you will have spaces at the end of the string to fill out all the length of the field. That is why you need TRIM to trim them out before you compare. ' Oct. 5' is different than 'Oct. 5'. Maybe that is the reason.

After adding TRIM, do you get the result you want?
 
Ah, I see. That makes sense. They are actually coming out of an Access Textfield.
 
if these are coming out of the database as a textfield, than that means you are doing string comparison instead of actual datetime comparison, and that any differences in the string will cause a not-equal comparison. in addition to puhctran's example pointing out that extra spaces can throw off this comparison, it also means that &quot;11-01-2003&quot; isn't the same as &quot;Nov 1, 2003&quot; or &quot;01-Nov-2003&quot; or &quot;November 1, 2003&quot;, etc...

if using string comparison, it's vital that all dates are in the exact same format. better yet, cast them to an actual datetime value using CDate(), or use VBScript's Month() Day() and Year() functions when comparing.

if you want to work with these values as strings, at the very bottom of this page you will find a function named FixDate() that might be very helpful. (this is an article i recall having read years ago, and i'm still using this exact function to this day!)

best of luck to you!
-f!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top