Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Clear the word count of a C# Sorted Dictionary

Posted on 2008-11-15
6
Medium Priority
?
439 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:shanemay
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:colonel720
ID: 22968014
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
 

Author Comment

by:shanemay
ID: 22968023
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
 
LVL 1

Expert Comment

by:colonel720
ID: 22968049
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:shanemay
ID: 22971636
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
 
LVL 1

Accepted Solution

by:
colonel720 earned 2000 total points
ID: 22971689
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
 

Author Closing Comment

by:shanemay
ID: 31517101
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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question