Re: Re; conditional replace
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Re; conditional replace
 
- From: "Juri Munkki" <jm_list@...>
 
- Date: 2007年9月25日 13:04:20 +0300 (EEST)
 
On Mon, September 24, 2007 11:19 pm, Thomas A. Schmitz wrote:
>> This works because ipairs() does bring out the "array-style"
>> entries (i.e. those with index 1..n) in a table in numeric order.
>
> Very cool, I will try that! Thanks
I think you'll find nested tables easier to maintain. You'll have one
extra table for each substitution, but I think that's a fair price for
some added clarity:
replacements = {
	{	"caterpillar", "butterfly" },
	{	"cat", "dog" },
	{	"goldfish", "parrot" }};
function translate(text)
	for i,v in ipairs(replacements) do
		text = text:gsub(v[1], v[2]);
	end
	return text;
end
print(translate("The cat looked at the caterpillar as if it had just
swallowed a goldfish."));
should print out:
 The dog looked at the butterfly as if it had just swallowed a parrot.
Swap the first two lines of the substitutions table to get:
 The dog looked at the dogerpillar as if it had just swallowed a parrot.
-- 
Juri Munkki