Get differences from 2 arrays

# Thread: Get differences from 2 arrays

1. Join Date
Dec 1969
Posts
67

## Get differences from 2 arrays

Problem.<BR>i have 2 arrays almost equal with about 100 values. Now I need to get the different values from the arrays and display them.<BR>Is there an easy way to do this?<BR>I was thinking of a script somewhat like this<BR><BR>for i = 0 to UBound(arrValueA)<BR> for j = 0 to UBound(arrValueB)<BR> If ArrValueA(i) = ArrValueB(j) Then<BR> ArrValueA(i) = True<BR> ArrValueB(j) = True<BR> End if<BR> Next<BR>If Not ArrValueA then<BR>response.write ArrValueA & "&#060;br /&#062;"<BR>End if<BR>if Not ArrValueB Then<BR>response.write ArrValueB & "&#060;br /&#062;"<BR>End if<BR>Next<BR><BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Are they sorted?

If they are sorted, this is a piece of cake.<BR><BR>If they aren&#039;t sorted, it will be slow and clumsy...and will take about as long as sorting them would. <BR><BR>If these are try 1-dimensional arrays and you sort them via JavaScript (see the ASPFAQs, category "Arrays") and then do a "merge" on them that would surely be the fastest thing.<BR><BR>

3. Join Date
Dec 1969
Posts
67

## RE: Are they sorted?

Yes the array&#039;s are sorted in alhpabetical order.<BR>in fact one array comes from an MySQL DB and contains all users in our phonelist. the other array contains all users in our domain extracted with adsi. so both arrays should contain at least 90% the same names but both arrays will also have differences. can you explain the merge thing?<BR><BR>thanks

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Perfect

elem1 = 0<BR>elem2 = 0<BR><BR>elem1max = UBound(array1)<BR>elem2max = UBound(array20<BR><BR>Do While True<BR> &#039; when we run out of elements in *both* arrays, we are done<BR> If elem1 &#062; elem1max AND elem2 &#062; elem2max Then Exit Do<BR> &#039; else get elements from both arrays...<BR> &#039; ... but if past max on one array, use dummy value<BR> If elem1 &#060;= elem1max Then v1 = array1(elem1) Else v1="zzzzzzzzzzzz"<BR> If elem2 &#060;= elem2max Then v2 = array2(elem2) Else v2="zzzzzzzzzzzz"<BR><BR> &#039; now compare: <BR> If v1 &#060; v2 Then<BR> Response.Write "Not found in array2: " & v1 & "&#060;br/&#062;"<BR> elem1 = elem1 + 1<BR> Elseif v1 &#062; v2 Then<BR> Response.Write "Not found in array1: " & v2 & "&#060;br/&#062;"<BR> elem2 = elem2 + 1<BR> Else<BR> Response.Write "In both arrays: " & v1 & "&#060;br/&#062;"<BR> elem1 = elem1 + 1<BR> elem2 = elem2 + 2<BR> End If<BR>Loop<BR>%&#062;<BR><BR>If VBS had "shortcut" AND operators, it could be done without the fake "zzzzzzz" values. Ehhh...It could be, anyway. Just uglier.<BR><BR><BR>

5. Join Date
Dec 1969
Posts
67

## RE: Perfect

Great this looks much nicer than the one I came up with after spending lots and lots of hours testing<BR><BR>If y_Applications &#060;&#062; "" Then<BR>If request("abuttonchoice") = "appdelete" then<BR> For i = 0 to UBound(ARR1)<BR> ARR1(i) = trim(ARR1(i))<BR> Next<BR> For j = 0 to UBound(ARR2)<BR> ARR2(j) = trim(ARR2(j))<BR> Next<BR> for i = 0 to ubound(ARR1)<BR> found = "false"<BR> for j = 0 to ubound(ARR2)<BR> if (ARR1(i) = ARR2(j)) then<BR> found = "true"<BR> exit for<BR> end if<BR> next<BR> if found ="false" then<BR> strOutput=strOutput & ARR1(i) & ","<BR> ARR1temp = ARR1(i)<BR> End If<BR> next<BR><BR>If Right(strOutput, 1) = "," Then<BR>strOutput = Left(strOutput, Len(strOutput)-1)<BR>End If<BR><BR>And this works only 1 way.<BR><BR>Thanks I will be using yours. saves a lot of trouble and lines of code.

6. Join Date
Dec 1969
Posts
67

## RE: Perfect

Your script looked perfect. But is not working for me yet. Eventhough the array&#039;s are sorted I still have some empty and duplicate records. And even if the values match you script says it is no match.<BR><BR>What could be the problem?

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## BUG BUG BUG!

LOL! Right amount of COPY/PAST, but not enough EDIT!<BR><BR>Didn&#039;t <BR> elem2 = elem2 + 2<BR>look suspicious to you??? When all the other "increments" were by 1?<BR><BR>That should be simply<BR> elem2 = elem2 + 1<BR><BR>**********************<BR><BR>Here&#039; s an improved version that handles arrays of either numbers or of strings. Please not that as written the string version is CASE SENSITIVE!<BR><BR>&#060;%<BR>&#039; comment out either of the pairs of array initializations to test:<BR>&#039;<BR>array1 = Array( 1, 3, 5, 7, 9, 11, 13, 15, 17 )<BR>array2 = Array( 2, 3, 4, 5, 9, 12, 14, 15 )<BR><BR>array1 = Array( "apple", "cantaloupe", "date", "eggplant", "fig", "honeydew", "iceberg lettuce" )<BR>array2 = Array( "banana", "cantaloupe", "date", "fig", "grape", "honeydew" )<BR><BR><BR>elem1 = 0 <BR>elem2 = 0 <BR><BR>elem1max = UBound(array1) <BR>elem2max = UBound(array2)<BR><BR>Do While True <BR> &#039; when we run out of elements in *both* arrays, we are done <BR> If elem1 &#062; elem1max AND elem2 &#062; elem2max Then Exit Do <BR> &#039; else get elements from both arrays... <BR> &#039; ... but if past max on one array, use dummy value <BR> If elem1 &#060;= elem1max Then v1 = array1(elem1) Else v1=NULL<BR> If elem2 &#060;= elem2max Then v2 = array2(elem2) Else v2=NULL<BR> &#039; Response.Write "(" & elem1 & ") " & v1 & " ? (" & elem2 & ") " & v2 & "&#060;br/&#062;"<BR> If IsNull(v2) Then v1Less = True Else v1Less = ( v1 &#060; v2 )<BR> If IsNull(v1) Then v2Less = True Else v2Less = ( v1 &#062; v2 )<BR><BR> &#039; now compare: <BR> If v1Less Then <BR> Response.Write "Not found in array2: " & v1 & "&#060;br/&#062;" <BR> elem1 = elem1 + 1 <BR> Elseif v2Less Then <BR> Response.Write "Not found in array1: " & v2 & "&#060;br/&#062;" <BR> elem2 = elem2 + 1 <BR> Else <BR> Response.Write "In both arrays: " & v1 & "&#060;br/&#062;" <BR> elem1 = elem1 + 1 <BR> elem2 = elem2 + 1<BR> End If <BR>Loop <BR>%&#062; <BR>

8. Join Date
Dec 1969
Posts
67

## RE: BUG BUG BUG!

Now you mention it and rereading the script your are completely right. I have one bad habbit and that is reading over errors. even minor once like forgetting a " or ) it always hunts me.<BR><BR>thanks this one works fine.

#### Posting Permissions

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