Link to home
Start Free TrialLog in
Avatar of shanemay
shanemayFlag for United States of America

asked on

Clear the word count of a C# Sorted Dictionary

I have an arraylist of sortedDictionaries.  Each dictionary represents the words in a text file.  After one text file is read, I load another one.  However, even though I clear the sortedDictionary the words are cleared, however, the count of that word is not cleared.  I am not sure what to do.   Below is the code that I run after each page is processed.  The words are correct, however, the counts are off, for example the word "example" appears twice on each page, but the count of the third dictionary shows the count as six.  The words, clear, but the count does not.  

Any help would be greatly appreciated.
pageDictionaries.Add(pageWords);
pageWords = new SortedDictionary<string, int>();
pageWords.Clear();

Open in new window

Avatar of colonel720
colonel720
Flag of United States of America image

I take it that code is in a loop - if you are instantiating a new SortedDictionary every time around, why do you need to clear the new object?
Avatar of shanemay

ASKER

I am using a loop, I thought instantiating a new SortedDictionary every time would clear the count, but is does not.  So I tried using the Clear method, the clears the words, but not the counts.  The clear method is not needed, I just tried it.  

Thank you for the reply.  
Hello again, shanemay :-)

Why are you using the word as the key in each sortedlist item?
Also, here is some test code that worked for me - at the end of the loop, each SortedDictionary's count is 10.:


List<SortedDictionary<int, string>> x = new List<SortedDictionary<int, string>>();
            for (int i = 0; i < 10; i++)
            {
                SortedDictionary<int, string> sd = new SortedDictionary<int, string>();
                for (int y = 0; y < 10; y++)
                { sd.Add(y, "this_is_a_word"); }
                x.Add(sd);
            }

Open in new window

Thank you for the response.  I think my approach is incorrect.  I have a large text file with each paragraph representing a "page" that needs it own dictionary.  How would it be best to loop through each paragraph? Each paragraph is separated by blank lines.  

Again, thank you for the response.  
ASKER CERTIFIED SOLUTION
Avatar of colonel720
colonel720
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for the response.  My approach was incorrect, I was only searching for words and was not spliting the document up by paragraph first.  Again, thank you I really appreciate your help.