[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Help with my horrible function...
- From: "John Batty" <john@...>
- Date: Tue, 12 Jun 2001 22:49:41 +0100
How about using the same sort of gsub() technique, but separating your data
from your code - something like this...
patterns = {
":{g}",
".{b}",
"%{B}" }
local i,v = next(patterns, nil)
while i do
local chr = strsub(v,1,1)
local prefix = strsub(v,2)
s = gsub(s, "(%"..chr.."+)", function(x) return %prefix..x end)
i,v = next(patterns, i)
end
----- Original Message -----
From: "Jason Clow" <ih8msft@kmfms.com>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: 12 June 2001 19:06
Subject: Help with my horrible function...
> Hi there,
> I have a function that takes a string, checks for sequences of one or more
characters, and inserts something in front of it. For example, if the string
is ":::...MMM", it will be changed to "{g}:::{b}...{x}MMM". I am using
gsub() to insert the text, using patterns like "(M+)", but I couldn't figure
out a pattern that would capture any sequence of characters. So, my function
basically looks like:
>
> s = gsub(s, "(:+)", function(l) return "{g}"..l end)
> s = gsub(s, "(%.+)", function(l) return "{b}"..l end)
> s = gsub(s, "(%%+)", function(l) return "{B}"..l end)
>
> ...and so on, for about 10 more patterns. This is quite bad, since I would
rather have another function that checks what the character is, then returns
the appropriate string, like this:
>
> function check(s)
> ss = strsub(s, 1, 1)
> if ss == ":" then return "{g}"..s
> elseif ss == "." then return "{b}"..s
> elseif ...
> ...
> end
>
> function blah(s)
> return gsub(s, "(some pattern)", check)
> end
>
> Is there a pattern that can be used to capture sequences of one or more
different characters?
>
> Regards,
> Jason
>
> _____________________________________________________________
>