Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deciphering an Error Message

Status
Not open for further replies.

MISMCSA

MIS
Jun 24, 2004
43
0
0
US
I get this error message after I try to run this script a second time, after a successful run the first time. Are there values still in the dictionary?

Error: This key is already associated with an element of this collection.

Code: 800A01C9

Main Part of Code Below....

Description: Will Compare a list of user ID's against one or two groups of user ID's (that are all stored in text files), and output a text file that contains the user ID's that were not part of either group list.

CODE (excerpt, if you want to see it all, let me know.)
Code:
Set Dictionary1 = CreateObject("Scripting.Dictionary")

Set ReadFile = fso.OpenTextFile(MasterList1, 1, false)
		Do While ReadFile.AtEndOfStream <> True
			str1 = Trim(ReadFile.ReadLine())
			Dictionary1.Add str1, True		
		Loop
		ReadFile.close

Set ReadFile = fso.OpenTextFile(StringList, 1, false)		
		strArray = Split(ReadFile.ReadAll, vbCrLf)		

For i = 0 to Ubound(strArray)
			If Not Dictionary1.Exists (Trim(strArray(i))) Then
				If fso.FileExists(OutputFile) Then			
					WriteFile.WriteLine strArray(i)
				Else
					Set WriteFile = fso.CreateTextFile(OutputFile, True)
					WriteFile.WriteLine strArray(i)
				End If
			End If
		Next
 
I may have found the problem. One of the files has four identical strings in it. I guess it's just telling me that that string is already in my dictionary.
 
I have a new question then. Perhaps someone has an idea of how to do this.

If I'm loading a list of strings into a dictionary object and there are duplicate values in the list, is there a way to have the dictionary ignore the duplicate and continue on?

Scenario:

Test 1
Test 2
Test 2
Test 3

Currently having two "Test 2"s will give me the error from my fist post. Is there a way to have it store only 1 "Test 2", so it doesn't puke.

Thanks for the help.
 
You already do this in your code at one place, you just need to do it everywhere:
Code:
If Not Dictionary1.Exists(str1) Then
    Dictionary1.Add str1, True

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Good point...early morning coding. The brain never functions at it peak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top