Search code examples
c#countsumreadlines

Read value from file, after 12 times ","


i have file, where i have separated values via "," - but it's not .csv extension.
I'm trying to get in C# sum from each row, which start from 0.

To this sum, should be taken always value after 12th of ","

0,P2CZ,1,0,400,400,802,802,18,18,1299,,5,248721,2487221,2
1,1,2487221,1,1_EG_3ST2_1/22,,0,460,18,0
0,,2,0,400,400,982,982,18,18,13999,,9,2321,248731,24878/121,24872021,,,,,,
1,1,2482021,1,1_EG_9_ST2_1/22,,0,460,18,0
1,3,2487821,1,1_EG_U9_ST2_1/22,,0,1042,18,0
1,4,248781,1,1_EG_U9_ST2_1/22,,0,1042,18,0
0,,3,0,2000,2000,80,80,18,18,14/0999,,1,,,,,,,,,,2000,80,1.3,,,,0,0,,,

So in this style:

  • 1st row, after 12th "," is value "5",
  • 2nd row skipped, because of 1 on the beginning,
  • 3rd row, after 12th "," is value "9",
  • 4-6 rows skipped, because of 1 on the beginning,
  • 7th row, after 12th "," is value "1"

So sum is 5 + 9 + 1 = 15.

I already input read lines with patch + data taken from label and + extension.

But what next ? How should i handle this ?

int count = File.ReadLines(patch + label97.Text + ".ext").Count(line => !line.All(char.IsWhiteSpace));

Solution

  • For starters, you should be doing this:

    File.ReadLines(patch + label97.Text + ".ext")
        .Where(line => !line.StartsWith("1") // get lines that do not start with 1
        .ToArray(); 
    

    Then, using the loop statement of your choice:

    var desiredPart = line.Split(new[] { ',' })[11]; // get whatever is at the twelveth position
    int value = int.Parse(desiredPart);
    

    Of course, you need to check that the Split actually gives you 12 positions and whether it's a number or not.