Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys RESOLVE
Esteemed Forum Participants and Lurkers:
===============================
Excel 2003 VBA
I am doing some relatively low level 'artificial intelligence', and I think the easiest way achieve my objective is to make a pair of dictionaries, and to iterate through 1 of the dictionaries checking for matching entries in the second dictionary. What I need to do in the case of a match, is to REMOVE both of the dictionary entries.
The question is: "is it safe to REMOVE dictionary entries WHILE ITERATING through the dictionary ???"
If this is UNwise, I need to rethink my whole approach.
Here is a code snippet:
Code:
For Each aKey In PROGd.keys
'Check for a matching key in the DISTRO Dictionary
If DISTd.exists(aKey) Then
'We are DONE with these keys, so delete them!
PROGd(aKey).Remove
DISTd(aKey).Remove
End If
Next aKey
It is actually a whole lot more complex than this code, but what I am doing is looking for exceptions where 2 lists do NOT match. When I get done iterating through 1 dictionary, the remainders in both dictionaries are all of the exceptions that I need to harpoon.
Thank you for any and all comments, suggestions, and assistance.
Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???
Im not 100% sure on a for each loop if you remove items from the collection if it would be safe but I do know that if you did a for i = dict.items.count - 1 to 0 it would be definately safe.
When you loop in reverse the removed items will not change the collection indexes so no "Item can not be found in collection" errors.
Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???
if you exit the for loop immediately after the deletion it should also be safe
Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???
I had to do a double take on your reply westconn1 :lol: For a second I thought you were the original poster. :D
Yup, if you exit out of the loop and perhaps re-enter it to reinitialize the collection loop that will work too but seem more work then just reverse looping.
Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???
it appeared to me, though maybe incorrectly, that an entry would only be in each dictionary one time, so if deleted no reason to continue the loop, just move to the next entry
Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???
This part of my application is an EXCEPTION HANDLER.
The 2 lists I am trying to reconcile may contain 200 or more items and most of the items will match and get deleted because they are correct and will be processed correctly. At the point I am at in the code, it is only the exceptions that I am interested in locating, since these are due to various errors and need to be corrected before the business process can be completed. The nice thing about the dictionaries is that each key can only be added to the dictionary one time, which preclues errors due to duplicates.
Thank you all for your gracious comments and assistance. I have a good handle on the solution now.