[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A little bit of golf
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 8 Apr 2015 09:55:55 -0300
> Well, it's the middle of the night and I can't sleep, but this passes
> all your tests, and all other tests posted in the thread so far
> (except "$undefined" expands to "" rather than "$undefined", as you
> requested):
>
> local function expand_variables(str)
> return (str:gsub("%$([_%w]+)", "${%1}"):gsub("%${([_%w]+)}", function (v)
> return variables[v] or "";
> end));
> end
That would be my answer, too, except that I would use "%b{}" in the
second gsub. (That changes a little the meaning; it allows arbitrary
"names" inside the brackets, as long as they have balanced brackets.)
-- Roberto