0

I have those lines of data:

1234;06/07/22;06/07/22;VRT;  ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT;  ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT;  ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT;  ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT;  ;31070;some String content;some String content; ;147.10;A;1234

I want to count how much time the letter A appeared in those lines it sould print 5.

So what I have a file with 19 lines and all those lines has letter A when I tried this snipped of code it returns to me just 12. here is the image for demonstration: enter image description here

I tried this code:

let nLines = 0;
let counter = 0;

for (let i = 0, n = this.csvRecords.length; i < n; ++i) {
  if (this.csvRecords[i] === '\n' && this.csvRecords.length[i][10] === 'A') {
    ++nLines;
    counter++;
  }

  console.log("number of lines in the file is: ", this.csvRecords.length) // this is giving me 19 which is right
}

console.log(counter) // this is giving me 0 which is wrong

So console.log( "showing u[10] is: " , this.csvRecords[10].length); is printing me the 10th line and it counts data with deliminer and that's not what i want I want how much it appeared in all the lines.

PR7
  • 1,524
  • 1
  • 13
  • 24
BAKHALED Ibrahim
  • 473
  • 1
  • 7
  • 19
  • it returns to me 0 I don't know why, I'll update the full code – BAKHALED Ibrahim Apr 16 '22 at 02:10
  • You can modify this solution to fit your usecase https://stackoverflow.com/a/881111/3199927 – Tom Apr 16 '22 at 02:22
  • Hey Tom, I tried that and changed my csv file to string and it gives me : `ERROR TypeError: csv.match is not a function` thet thing is that the letter `A` is in each row that means is there a way to count in `Y` not `X` – BAKHALED Ibrahim Apr 16 '22 at 02:30
  • PHP has built-in command `substr_count()`. You can use that for the individual letter per CSV line, to tally the count. Ref https://www.php.net/manual/en/function.substr-count.php – mardubbles Apr 16 '22 at 03:49

0 Answers0