lua-users home
lua-l archive

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


2015-05-29 20:25 GMT+02:00 Lionel Duboeuf <lionel.duboeuf@gmail.com>:
> hello you all,
>
> Just in case i'm doing it not efficiently and to learn best practices:
> I have a character stream that is formated like this one:
>
> ...<6 orange/> <2 20/> <1 1/> <2 20/> <5 false/> <1 0/> <16 orange
> mechanics/> <2 25/>...
>
> which correspond to a row column format like this
> t = {
>     {  "col1" = "orange" ,  "col2" = 20  },
>     {  "col1" = 1 ,  "col2" = 20  },
>     {  "col1" = false ,  "col2" = 0  },
>     {  "col1" = "orange mechanics" ,  "col2" = 25  },
> ...
> }

> any advices will be very appreciated.

I usually do it with gsub. Not my own idea, I saw it in
some code of Roberto's.

t = {}
data:gsub(pattern,
   function(captures)
      t[#t+1] = do_something_with_captures
   end)

In yiour example, where you want numbers and booleans
to be translated, I would do this:

item = (function ()
   local code = load(item)
   if code then return code() end
end)() or item

If "item" is valid Lua code, it is replaced by its value, otherwise
it stays unchanged.