[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string.gsub (Lua 5.1.1)
- From: roberto@... (Roberto Ierusalimschy)
- Date: Tue, 7 Nov 2006 15:09:11 -0200
> Counting matches instead of replacements IMHO is simpler to understand
> and use. It also provides an easy way to count how many times a given
> pattern occur in a string.
>
> path= "/many/nested/directories"
> _,n= path:gsub("/",{}) -- n counts "/" chars
I do agree that it is simpler, and that is the main reason we chose it.
But I am not sure it is "better". You can use other options for this
code, like
_,n= path:gsub("/","")
On the other hand, counting only replacements allow some interesting
tricky counts:
-- count number of reserved words in string
_,n = string.gsub(s, "%w", {[while] = true, [for] = true, ...})
-- Roberto