[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to count the number of lines in a string?
- From: "Patrick Donnelly" <batrick.donnelly@...>
- Date: Thu, 31 Jul 2008 06:36:34 -0600
On Thu, Jul 31, 2008 at 5:47 AM, Jerome Vuarand
<jerome.vuarand@gmail.com> wrote:
> 2008/7/31 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>>> what is the most efficient way of counting the number of lines?
>>
>> Try this one:
>>
>> local n=0
>> for i in s:gmatch("\n") do n=n+1 end
>> print(n)
>
> Also:
>
> local n = select(2, s:gsub('\n', '\n'))
>
You can make it so just an empty string is pushed if there is at least
one newline:
local n = select(2, s:gsub("[^\n]*\n[^\n]*", ""))
I haven't benchmarked it though.
Cheers,
--
-Patrick Donnelly
"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."
-Will Durant