[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: Duncan Cross <duncan.cross@...>
- Date: Sat, 28 Nov 2015 23:46:46 +0000
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?
-Duncan