This question has already been answered:

A 1000-line text file is given, which is read line by line and split by commas, and written to an array, an example of lines in it.

001,03,043,04/07/15 16:02,00:00:25,00,00,00,Indexer #1 shot pin state is unknown 002,02,030,09/24/15 16:39, 00:00:04,00,00,01,ATT unable to detect tray at Gripper 

If it is in the second element, I calculated the number of 01,02,03,04,05,06 knots with the help of the switch case 01,02,03,04,05,06 , and how in the third position 01,02,03,04,05,06 number of those or other errors count if their numbers vary from one to one hundred?

Reported as a duplicate by members Bald , xaja , ermak0ff , Dmitry D. , PashaPash 5 Oct '15 at 12:26 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • those. Do you want to get how many times do you have 043, 030? - Bald
  • one
    take a decision without a switch of your previous question - he doesn’t care what column to count - PashaPash
  • No, this is an example of only two lines, and there are a thousand of them, and numbers from 01 to 100. - Alex
  • one
    I give a hint: there it is necessary to change [1] to any other index and the number of such values ​​will be returned - Bald
  • And if we do not know the error codes? - Alex

2 answers 2

You can use a Dictionary :

 Dictionary<string, int> dictionary = new Dictionary<string, int>(); foreach (string item in myArray) { string[] elements = item.Split(','); if (dictionary.ContainsKey(elements[2])) { dictionary[elements[2]] ++; } else { dictionary.Add(elements[2], 1); } } foreach (KeyValuePair<string,int> dict in dictionary) { Console.Write("В тексте {0} ошибок типа {1}",dict.Value, dict.Key); } 
     var lines = File.ReadAllLines("data.txt"); var counter = new DictionaryCounter<string>(); var list = lines.Select(x => x.Split(',')[1]).ToList(); list.ForEach(counter.Increment); counter.ForEach(x => Console.WriteLine("error={0}\tcount={1}", x.Key, x.Value)); 

    We describe the class DictionaryCounter :

     public class DictionaryCounter<T> : Dictionary<T, int> { public void Increment(T key) { Increment(key, 1); } public void Increment(T key, int incValue) { if (ContainsKey(key)) this[key]++; else Add(key, incValue); } public void ForEach(Action<KeyValuePair<T, int>> action) { foreach (var item in this) { action(item); } } }