Am 29.09.2011 19:46, schrieb Robert:
Hi,

sorry, it may be a bit off topic. But I hope someone can help me with a small problem.

I have two lists that I want to compare for differences between. Both lists have some thousands of already sorted entries. I colud solve it using two for-loops inside each other. For small lists this is ok. But not for so many values.

Both lists are structured like this an only the keys should be compared (is there a new key or one missing?):

   a1['jimmy']   = { name : 'Jim', ... }
   a1['johndoe'] = { name : 'John', ... }
   a1['robert']  = { name : 'Robert', ... }

   a2['chris']   = { name : 'Christian', ... }
   a2['johndoe'] = { name : 'John', ... }
   a2['robert']  = { name : 'Robert', ... }
   a2['will']    = { name : 'William', ... }

Now I just need the differend keys (new/missing) compared a1 to a2.

The solutions I found seem to be to complex for my case. I need a simple an effcient way to get the differences.


Might someone help me?


Tnx in advance,
Robert


Well, if they are sorted (for example alphabetically), an asyncronous loop would be much better I think. Therefore, you define 2 index variables outside of the loop, initialized with the first indizes of the arrays. Then you do a while-loop until one of the two indizes is equal to its array's length (all elements checked). Your comparison inside the loop takes advantage of that the arrays are sorted. In this case, it is absolutely clear where equal elements have to be: given that they are sorted alphabetically, you do not have to look from B down to Z to find matching elements to "Alex", do you?

So the idea is to compare the current elements, and if they are equal, do whatever you do in such cases and then increment both indizes, otherwise increment only the index of the array whose current element is less than the element of the other array. So to say, if the one array is (A, B, C, D, J, O, R) and the other is (C, E, J, M, R, T), your comparison is like this:

http://lwchris.lw.funpic.de/oldcontent/ComparisonRawDraft.txt

Much more efficient.

Hope that helps, Chris

--
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/greasemonkey-users?hl=en.

Reply via email to