[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A little bit of golf
- From: Matthew Wild <mwild1@...>
- Date: Wed, 8 Apr 2015 15:21:58 +0100
On 8 April 2015 at 13:55, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> 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.)
I wrote %b{} first, but it wasn't clear that it fitted Hisham's
intentions (only matching identifier-like names). I think in practice
either work though.
Regards,
Matthew