lua-users home
lua-l archive

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

On 3 August 2015 at 13:12, Sean Conner <> wrote:
> I just want a way
> to pull in a field name, and set that field in a table, all using LPeg,
> ideally without external variables [2].  Oh, and there is no defined order
> to the fields---they can come in any order (and for this problem, assume no
> duplicates).

I frequently solve this using rawset.
There's even an example in the lpeg documentation:

local space =^0
local name = lpeg.C(lpeg.alpha^1) * space
local sep = lpeg.S(",;") * space
local pair = lpeg.Cg(name * "=" * space * name) * sep^-1
local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
t = list:match("a=b, c = hi; next = pi")  --> { a = "b", c = "hi", next = "pi" }

> Each pair has the format name = name followed by an optional separator (a comma or a semicolon). The pair pattern encloses the pair in a group pattern, so that the names become the values of a single capture. The list pattern then folds these captures. It starts with an empty table, created by a table capture matching an empty string; then for each capture (a pair of names) it applies rawset over the accumulator (the table) and the capture values (the pair of names). rawset returns the table itself, so the accumulator is always the table.