PDA

View Full Version : Comparing Array Values


KingB24
06-10-2003, 07:12 PM
I need help fast with a validation issue which is probably pretty easy:

I have an array of dates:

DateArray(31)

Once the array is populated I need to check to see if any of the dates in the positions are the same as any of the other dates in the other positions. If so I need to display an error. I am struggling with trying to figure this out. Please help

KingB24
06-10-2003, 07:15 PM
The format of the dates in the array are

MM/DD/YYYY

ex: 6/10/2003

or 10/6/2003

raf
06-10-2003, 08:15 PM
So the values in the array are in MM/DD/YYYY, and the values can be in MM/DD/YYYY or DD/MM/YYYY ?

then you can us

dim i, dateorig, datetranf1, datetranf2
i = 0
dateorig=request.form("date") 'get the value from somewhere
datetranf1 = CDate(Day(dateorig) & "/" & Month(dateorig) & "/" & Year(dateorig))
datetranf2 = CDate(Month(dateorig) & "/" & Day(dateorig) & "/" & Year(dateorig))

do while i <=35
if datetranf1 = DateArray(i) or datetranf2 = DateArray(i) then
response.write("Error. Date already in collection.")
end if
i = i + 1
loop


Or something like that. Maybe the CDate isn't necesarey

KingB24
06-10-2003, 08:35 PM
The problem is I need to compare values of an array to values in other positions within the same array. So if I have 6/10/2003 in position 1 of the array and I also have 6/10/2003 in position 3 of the same array, I need to display the error

raf
06-10-2003, 09:54 PM
I'd check if the date already occurs before entering a nex date into the array. Else you'll probably need to make about 35 * 35 * 36 comparisons, which might take a while ...
This is how it can be done (using a nested loop)

dim i, j, dateorig, datetranf1, datetranf2
i = 0

dateorig=request.form("date") 'get the value from somewhere
datetranf1 = CDate(Day(dateorig) & "/" & Month(dateorig) & "/" & Year(dateorig))
datetranf2 = CDate(Month(dateorig) & "/" & Day(dateorig) & "/" & Year(dateorig))

do while i <=35
j = 0
dateorig=DateArray(i) 'get the value from the array
datetranf1 = CDate(Day(dateorig) & "/" & Month(dateorig) & "/" & Year(dateorig))
datetranf2 = CDate(Month(dateorig) & "/" & Day(dateorig) & "/" & Year(dateorig))
do while j <= 35
if j <> i then
if datetranf1 = DateArray(j) or datetranf2 = DateArray(j) then
response.write("Error. Date already in collection.")
end if
end if
j = j + 1
loop
i = i + 1
loop

Not tested but should work
Let us know if you have problems/questions

glenngv
06-16-2003, 07:40 AM
as raf said, check first if the date already occurs before adding it to the date array.

Dim DateArray(31)
Dim DateToAdd
Dim TempArray

DateToAdd = CDate("10/6/2003")

TempArray = Filter(DateArray,DateToAdd,True)
if ubound(TempArray)>=0
response.write("Error. Date already in collection.")
else
'add to DateArray
end if


for more info on Filter function, click this (http://www.devguru.com/Technologies/vbscript/quickref/filter.html).