lua-users home
lua-l archive

Re: conditional replace

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sep 18, 2007, at 5:31 AM, Patrick Donnelly wrote:
It would probably be better to just let everything be done via stdin
and stdout, using pipes:
========
#!/usr/local/bin/lua
file = io.stdin:read("*all");
file = string.gsub(file, "localgreek(%b{})", function(lines)
 return "localgreek"..string.gsub(lines, 'a', 'b');
 end);
io.stdout:write(file);
=======
You will probably want to have the "return "localgreek" ..." part of
the code just call a function which iterates over your table of
substitutions for each capture. Notice that you need the "localgreek"
part concatenated otherwise you lose it.
HTH,
-- -Patrick Donnelly
Yes, this and Tomás's answer have helped a lot. I've got the function- with-gsub and the iterator working, but I've hit another snag: I build my array of substitutions like so:
subs = {
a = "α",
b = "β"
}
etc. But as soon as I try to stick some non-alphanumerical characters in there like so:
>~h| = ᾖ
I get errors. Is it impossible to have keys to an array that are of this form, or am I missing something obvious?
Thanks again!
Thomas

AltStyle によって変換されたページ (->オリジナル) /