Dheeraj Sharma
Dheeraj Sharma

Reputation: 13

I want to return unique value counts in C# through input text file

suppose there are lines like:

England A team player : Flintoff
England A team player : Flintoff
England B team player : Strauss
England A team player : Strauss
India team player A: Sachin Tendulkar
India team player B : Sachin Tendulkar
India team player A: Javagal Srinath

Now what i want is to search and return for unique value counts like if i want to search for England players unique counts, it should give me 2 as in above example.

Code i have tried, but is not working :

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}
List<string> users = new List<string>();
Regex regex = new Regex(@"player:\s*(?<playername>.*?)\s+appGUID");
MatchCollection matches = regex.Matches(searchKeyword);
foreach (Match match in matches)
{
    var user = match.Groups["username"].Value;
    if  (!users.Contains(user)) users.Add(user);
}
int numberOfUsers = users.Count;
Console.WriteLine(numberOfUsers);
// keep screen from going away
// when run from VS.NET
Console.ReadLine();

Upvotes: 0

Views: 689

Answers (4)

Manish Mishra
Manish Mishra

Reputation: 12375

  1. You need to check whether your list already contains the keyword
  2. Use dictionary collection.
  3. You won't need that regex thing.

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
Dictionary<string,int> results = new Dictionary<string,int>;
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        if(results.Keys.Any(searchKeyword))
        {
            results[searchKeyword]++;
        }
        else
        {
            results.Add(searchKeyword,1);
        }
        results.Add(line);
    }
}

foreach(var item in results)
{
    Console.WriteLine("Team:"+item.Key +"\nPlayer Count:"+item.Value);
}

Upvotes: 0

RMalke
RMalke

Reputation: 4094

A simpler way would be to use LINQ:

string searchKeyword = "England";
string fileName = @"C:\Users\renan.stigliani\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);

int numberOfUsers = textLines
                        .Where(x => x.Contains(searchKeyword))
                        .Distinct()
                        .Count();

Console.WriteLine(numberOfUsers);

// keep screen from going away
// when run from VS.NET
Console.ReadLine();

As noted by @DominicKexel I swept the foreach

Upvotes: 1

Tim
Tim

Reputation: 8921

If you are not wedded to the Regex approach, you could add the values (e.g. "England player: Foo") to a Dictionary object, making the value the key so duplicates don't get added, and then use the Count method. See also the ContainsKey method.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Upvotes: 0

Tom
Tom

Reputation: 984

Might sound a little simplistic but why don't you just select distinct from your list? Example:

string searchKeyword = "England";
string fileName = @"C:\Users\karansha\Desktop\search tab.txt";
string[] textLines = File.ReadAllLines(fileName);
List<string> results = new List<string>();
foreach (string line in textLines)
{
    if (line.Contains(searchKeyword))
    {
        results.Add(line);
    }
}
List<string> users = results.Distinct().toList();

That will give you unique lines, and you need to split, you can do it easily. You can know how many unique with the count.

Upvotes: 0

Related Questions