Re: Compare the values of two sorted arrays of variable size.



Andy,

This algorithm does not check for every possible location in each array.
Your easiest way to check would be 2 loops (like the first idea you had)

grab one item on the first array and compare it against all items on the
second array, move to the second item and compare it against all items on
the second array and so on.

if you need an example, let me know. if you need an example of why the code
you have does not work as expected, let me know and i explain it to you.
RT

"Andy" <andy.and.suzanne.cooper@xxxxxxxxx> wrote in message
news:1119306333.719399.87100@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> This seems a bit of an old chestnut but please humour me ;)
>
> My aim is to compare two arrays and identify values in List A which
> don't appear in List B. And vice versa. My arrays are one dimensional
> and contain names. The number of elements in each array is not fixed.
>
> I've done some searching and there are two basic algorithms. The first
> contains a double loop where each element of the inner loop is compared
> to elements in the outer loop. At best it kicks out of the inner loop
> when a match is found.
>
> The second method seems to be more elegant but assumes the two arrays
> have already been sorted. Basically the indeces of the two arrays are
> incremented when an unmatched value is found. An example code can be
> found below which works ... until you place a unique value as the last
> element :(
>
> For example, if I have:
>
> ListA = Array("Andy", "Bob", "Cath", "Dexter")
> ListB = Array("Andy", "Bob", "Cath")
>
> BOTH "Cath" and "Dexter" are found to be unique! I switched the values
> of list A to B and came up with the same poor result.
>
> I'd appreciate it if someone could give this a fresh look and find the
> logic error. I've been staring at it for hours :(
>
> Many thanks in advance,
>
> Andy
>
> Script:
> Option Explicit
>
> Dim ListA ' To contain a sorted list of names, unknown size.
> Dim ListB ' To contain a sorted list of names, unknown size.
> Dim lngAIdx ' Index for list A.
> Dim lngBIdx ' Index for list B.
> Dim lngMaxAIdx ' Upper value of the A list index.
> Dim lngMaxBIdx ' Upper value of the B list index.
> Dim intComparison ' Result of comparing two strings
> '
> ListA = Array("Andy", "Bob", "Cath", "Dexter")
> ListB = Array("Bob", "Cath", "Dexter")
>
> lngAIdx = LBound(ListA)
> lngBIdx = LBound(ListB)
> lngMaxAIdx = UBound(ListA)
> lngMaxBIdx = UBound(ListB)
> '
> Do While (lngAIdx < lngMaxAIdx And lngBIdx < lngMaxBIdx)
> 'Perform a case insensitive comparison.
> intComparison = strComp(ListA(lngAIdx), ListB(lngBIdx), vbTextCompare)
> If intComparison = -1 Then ' ListA(lngAIdx) < ListB(lngBIdx)
> ' Value in A list is not present in the B list.
> WScript.Echo "Unique A Value: " & ListA(lngAIdx)
> lngAIdx = lngAIdx + 1 ' Update the A list index.
> ElseIf intComparison = 1 Then ' ListA(lngAIdx) > ListB(lngBIdx)
> ' Value in B list is not present in the A list.
> WScript.Echo "Unique B Value: " & ListB(lngBIdx)
> lngBIdx = lngBIdx + 1 ' Update the B list index.
> Else ' intComparison = 0
> ' The value appears in both lists
> lngAIdx = lngAIdx + 1
> lngBIdx = lngBIdx + 1
> End If
> Loop
> '
> 'Having reached this point, one of the lists has finished.
> 'Display the remaining unique values.
> If lngAIdx < lngMaxAIdx Then
> For lngAIdx = lngAIdx to lngMaxAIdx
> WScript.Echo "Remaining Unique A value: " & ListA(lngAIdx)
> Next
> End If
> '
> If lngBIdx < lngMaxBIdx Then
> For lngBIdx = lngBIdx To lngMaxBIdx
> WScript.Echo "Remaining Unique B value: " & ListB(lngBIdx)
> Next
> End If
>
> WScript.Quit
>


.



Relevant Pages

  • Re: [opensuse] bash - why doesnt the tldp bash beginners guide for loop example work?
    ... I have run into a bash problem with a simple for loop I don't ... The echo statement showed that $i was receiving the ... I personally like to use array variables in BASH scripting. ... lists to operate on. ...
    (SuSE)
  • Compare the values of two sorted arrays of variable size.
    ... My arrays are one dimensional ... contains a double loop where each element of the inner loop is compared ... one of the lists has finished. ... If lngAIdx < lngMaxAIdx Then ...
    (microsoft.public.scripting.vbscript)
  • Re: wildcard arrays or lists
    ... lists of IP's. ... Is it possible to use wildcards in an array like ... only option to fill the array "by hand" with a loop with every single IP? ...
    (comp.lang.ruby)
  • Re: multiple input boxes
    ... if you're going through a loop ... will create a $_POST var that looks like this when submitted ... $_POST = array( ... Each line lists out the id, ...
    (comp.lang.php)
  • Re: list.clear() missing?!?
    ... 100 loops, best of 3: ... you need space for two lists: ... To be exact the reason for two array is timeit.py. ... But that is precisely the same for the other timeit tests too. ...
    (comp.lang.python)