Row count

I am trying to count the number of times a "-" occurs in a string.

So, for example, this happens twice here 'a -b -c'

I tried the following, but it gives me 4 instead of 2, any idea why?

argv='a --b --c' count = 0 for i in string.gfind(argv, " --") do count = count + 1 end print(count) 
+7
source share
2 answers

Symbol - has special meaning in patterns used for non-greedy repetition.

You need to run away from him, i.e. use the pattern " %-%-" .

+7
source

you can do it as a single line using string.gsub :

 local _, count = string.gsub(argv, " %-%-", "") print(count) 

no cycle required!

It is not recommended for large files, because the function stores the input of the variable _ and will be held in memory until the variable is destroyed.

+22
source

All Articles