Compare two lists
Blitz3D Forums/Blitz3D Programming/Compare two lists
| ||
Wow, do I have a stupid question. This is like programming 101 but I am completely at a loss on how to do this the "right way". I wish to compare two list and only want to count the number of similarities between them. Once a match item in both list is found, that same item will not be counted again, unless another occurrence is found in both list. If "A" is in the first list 4 times and once in the second list, the total should be 1. More examples: List A List B ------- ------- A A B B C C D B Here there would be 3 matches List A List B ------- ------- A C B B B B C A Here there would be 4 matches This is my approach, it works but I don't think it looks very professional. Also, I would like this to be a function if possible. Any ideas ? Graphics 800,600,0,2 Dim list_a$(100) Dim list_b$(100) Dim list_match(100) list_a$(1) = "A" list_a$(2) = "B" list_a$(3) = "C" list_a$(4) = "D" list_b$(1) = "D" list_b$(2) = "C" list_b$(3) = "D" list_b$(4) = "A" c = 0 For x = 1 To 4 For y = 1 To 4 If list_a$(x) = list_b$(y) ; Found a match If list_match(y) = 0 ; Has it been counted? list_match(y) = 1 ; If not, flag it Print list_a$(x)+" = "+list_b$(y) c = c + 1 ; Increment counter Exit End If End If Next Next Print "Total Matches: " + c WaitKey Last edited 2012 |
| ||
The Print statement in the loop is there for me to visually see when a match was found. It need not be there in the actual function / program. I must apologize, while I am using Blitz3D but I probably have posted this in the Blitz 3D Beginners Area forum. Last edited 2012 |
| ||
Hello, I've not tested this (not at a computer with blitz at present) but this should do the trick: Type ListA Field svalue$ Field matched% End Type Type ListB Field svalue$ Field matched% End Type ;set some values for the lists as an example ;using your example in your first post: la.ListA = New ListA la\svalue = "A" la.ListA = New ListA la\svalue = "B" la.ListA = New ListA la\svalue = "C" la.ListA = New ListA la\svalue = "D" lb.ListB = New ListB lb\svalue = "A" lb.ListB = New ListB lb\svalue = "B" lb.ListB = New ListB lb\svalue = "C" lb.ListB = New ListB lb\svalue = "B" Function ListMatchCount() Local count For la.ListA = Each ListA For lb.ListB = Each ListB if lb\matched = false then if la\svalue = lb\svalue then lb\matched = true count = count + 1 endif endif Next Next Return count End Function |
| ||
Didn't quite work. Made some adjustments and I believe it may be giving the proper results. Need to test some more before I know for sure. Let you know after a bit. Thanks. |
| ||
Ahh..looking at it again an "exit" statement after the count increment should do it. |