• Subject: Re: lpeg accumulator
• From: Hisham <h@...>
• Date: Wed, 18 Sep 2013 17:15:58 -0300

```On 18 September 2013 16:17, Gavin Wraith <gavin@wra1th.plus.com> wrote:
> I tried to use lpeg.Cf with a non-numeric accumulator (in
> lpeg 0.12) thus:
>
>   #!lua
>   local digit = lpeg.R "09"
>   local num = lpeg.C(digit^1)/tonumber
>   local comma = lpeg.C ","
>   local acc = function (t,x,y)
>                 t[1] = t[1] + x
>                 t[2] = t[2] + y
>               end
>   local vec = lpeg.Cf (num * comma * num, acc)
>   local pat = lpeg.Cc { 0, 0} * (vec + 1)^0
>   local t = pat:match "Try (3,4) plus (100,200)"
>   print ("(", t[1], ",", t[2], ")")

I think this does what you want:

local digit = lpeg.R "09"
local num = lpeg.C(digit^1)/tonumber
local comma = lpeg.P ","
local acc = function (t, pair)
t[1] = t[1] + pair[1]
t[2] = t[2] + pair[2]
return t
end
local vec = lpeg.Ct( num * comma * num ) -- capture pair
local pat = lpeg.Cf( lpeg.Cc {0,0} * (vec + 1)^0 , acc)
local t = pat:match "Try (3,4) plus (100,200)"
print ("(", t[1], ",", t[2], ")")

-- Hisham

```