lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 29 November 2015 at 10:14, Paul Merrell <marbux@gmail.com> wrote:
> Hi, All,
>
> 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?
>
> Best regards,
>
> Paul
>
> --
> [Notice not included in the above original message:  The U.S. National
> Security Agency neither confirms nor denies that it intercepted this
> message.]
>

Table keys are unordered.
If you need order; you'll need to do it yourself via using the ordered
property of integer keys.
i.e. use a list!

local tSub = {
   {patt = "%-%-%-"; repl = "---"};
   {patt = "%-%-"; repl = "--"};
   {patt = "%.%."; repl = "..."};
}