[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to return table key/value pairs in a particular order?
- From: Sean Conner <sean@...>
- Date: Mon, 30 Nov 2015 14:25:07 -0500
It was thus said that the Great Duncan Cross once stated:
> On Sat, Nov 28, 2015 at 11:14 PM, Paul Merrell <marbux@gmail.com> wrote:
> > I have a prototype table for substituting character strings with UTF8
> > characters:
> >
> > local tSub = {
> > ["%-%-%-"] = "—",
> > ["%-%-"] = "–",
> > ["%.%.%."] = " … "}
> >
> > My problem is that the first key/value pair must be processed before
> > the second key/value pair because of overlapping characters in the
> > keys, but the order in which Lua returns table key/value pairs is
> > unpredictable, leading to buggy results in the substitutions.
> >
> > Eventually the script would make many other substitutions, but I seem
> > to be stuck here at the prototype stage because of the unpredictable
> > return order.
> >
> > How might I work around this?
>
> To address this specific case instead of the general question of
> ordered key/value pairs in Lua: Assuming the keys and values are being
> used as the "pattern" and "repl" parameters to a string.gsub() call, I
> would try replacing the first two entries with this one:
>
> ["%-%-%-?"] = {["---"]="—", ["--"]="–"},
>
> Are there many other instances where order is important?
It's only important when a shorter match would match a longer match, such
with the dashes above. There are potentially other patterns too. My
blogging software [1] uses the following abbreviations:
--- —
-- –
... …
.. ‥ -- I'm not sure if this has a name or not
The only other type of thing might be TLAs [2] or ETLAs [3], like PCM
(Pulse coded modulation) and PCMCIA (People Can't Memorize Computer Industry
Acronyms). If you wanted to extend those out [4], you would have to list
PCMCIA first, or you may get Pulse Coded Modulation Central Intelligence
Agency, which may not be what you wanted.
-spc
[1] https://github.com/spc476/mod_blog
Written in C, uses Lua only for configuration.
[2] Three Letter Acronym
[3] Extended Three Letter Acronym
[4] For example, like
<acronym title="Cooking Institute of America">CIA</acronym>