Note that there are some explanatory texts on larger screens.

plurals
  1. POc# counting identical strings from text file
    primarykey
    data
    text
    <p>I have a <code>foreach</code> statement where I go through several lines from a text file, where I have trimmed down and sorted out the lines I need. What I want to do is count up on how many times an identical string is present. How do I do this?</p> <p>Here is my code. It's the second <code>if</code> statement where I am stuck:</p> <pre><code> foreach (string line in lines.Where(l =&gt; l.Length &gt;= 5)) { string a = line.Remove(0, 11); if ((a.Contains(mobName) &amp;&amp; a.Contains("dies"))) { mobDeathCount++; } if (a.Contains(mobName) &amp;&amp; a.Contains("drops")) { string lastpart = a.Substring(a.LastIndexOf("drops")); string modifiedLastpart = lastpart.Remove(0, 6); } </code></pre> <p>Heres what some of the lines look like:</p> <p>a bag of coins</p> <p>a siog brandy</p> <p>a bag of coins</p> <p>a bag of coins</p> <p>the Cath Shield</p> <p>a tattered scroll</p> <p>So what im trying to do is counting up there are 3 lines with bag of coins. But i need to make it so that it can be everything, theres a drop lists thats huge. So cant add all of em, would take too long</p> <p>EDIT</p> <pre><code> private static void Main() { int mobDeathCount = 1; int lootCheckCount = 1; string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Michael\Documents\Electronic Arts\Dark Age of Camelot\chat.log"); Console.WriteLine( "Enter which mob you want to see, remember to include the, for an example; The siog seeker, remember to start with a capital T"); string mobName = Console.ReadLine(); foreach (string line in lines.Where(l =&gt; l.Length &gt;= 5)) { string a = line.Remove(0, 11); if ((a.Contains(mobName) &amp;&amp; a.Contains("dies"))) { mobDeathCount++; } if (a.Contains(mobName) &amp;&amp; a.Contains("drops")) { string lastpart = a.Substring(a.LastIndexOf("drops")); string modifiedLastpart = lastpart.Remove(0, 6); var lineCountDict = modifiedLastpart.GroupBy(x =&gt; x).Where(x =&gt; x.Count() &gt; 1).ToDictionary(x =&gt; x.Key, x =&gt; x.Count()); foreach (var val in lineCountDict) { Console.WriteLine(val.Key + " - " + val.Value); } </code></pre> <p>new lines;</p> <p>[01:09:55] The siog seeker drops a bag of coins.</p> <p>[01:09:55] The siog seeker drops a siog brandy.</p> <p>[01:09:55] The siog seeker dies!</p> <p>[01:09:55] You get 3,687,564 experience points.(1,638,917 camp bonus)</p> <p>[01:10:31] You cast a Lesser Disenchanting Eruption spell!</p> <p>[01:10:31] You hit the siog seeker for 424 (+18) damage!</p> <p>[01:10:31] The siog seeker drops a bag of coins.</p> <p>[01:10:31] You pick up 18 silver and 88 copper pieces.</p> <p>[01:10:31] The siog seeker dies</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload