I have a foreach
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?
Here is my code. It's the second if
statement where I am stuck:
foreach (string line in lines.Where(l => l.Length >= 5))
{
string a = line.Remove(0, 11);
if ((a.Contains(mobName) && a.Contains("dies")))
{
mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);
}
Heres what some of the lines look like:
a bag of coins
a siog brandy
a bag of coins
a bag of coins
the Cath Shield
a tattered scroll
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
EDIT
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 => l.Length >= 5))
{
string a = line.Remove(0, 11);
if ((a.Contains(mobName) && a.Contains("dies")))
{
mobDeathCount++;
}
if (a.Contains(mobName) && a.Contains("drops"))
{
string lastpart = a.Substring(a.LastIndexOf("drops"));
string modifiedLastpart = lastpart.Remove(0, 6);
var lineCountDict = modifiedLastpart.GroupBy(x => x).Where(x => x.Count() > 1).ToDictionary(x => x.Key, x => x.Count());
foreach (var val in lineCountDict)
{
Console.WriteLine(val.Key + " - " + val.Value);
}
new lines;
[01:09:55] The siog seeker drops a bag of coins.
[01:09:55] The siog seeker drops a siog brandy.
[01:09:55] The siog seeker dies!
[01:09:55] You get 3,687,564 experience points.(1,638,917 camp bonus)
[01:10:31] You cast a Lesser Disenchanting Eruption spell!
[01:10:31] You hit the siog seeker for 424 (+18) damage!
[01:10:31] The siog seeker drops a bag of coins.
[01:10:31] You pick up 18 silver and 88 copper pieces.
[01:10:31] The siog seeker dies