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

shanemayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

colonel720Commented:
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?
0
shanemayAuthor Commented:
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.  
0
colonel720Commented:
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

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

shanemayAuthor Commented:
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.  
0
colonel720Commented:
find an instance of more than 1 consecutive line terminator (Environment.NewLine).
string[] paragraphs = System.Text.RegularExpressions.Regex.Split(document_text, Environment.NewLine + Environment.NewLine);
foreach (string para in paragraphs)
{
  //paragraph level operation
  foreach (string word in para)
  {
     //word level operation
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shanemayAuthor Commented:
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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.