I am trying to collect a range of Data from a string, that holds Data Values
example..
assert(Table.Ammo.Signature == 'AMMO')
assert(Table.Ammo.['Data Size'] == 194)
If anyone has any input on how to help me, i would appreciate it alot
IMHO, LPEG is a bit overkill. Lua regexp support is enough here.
Two versions: simple list and multi-level tree.
-----------------------------------
-- get simple list local Output = {}
for indent, line in Ammo:gmatch'( *)(.-)\n' do local name, value = line:match'^(.-): (.*)$' if name then name = name:match'^(.-) %-' or name -- make names short
Output[name] = value end end
-- get multi-level tree
local Output = {} -- result table local Stack = {{ind = 0, tab = Output}}
for indent, line in Ammo:gmatch'( *)(.-)\n' do while Stack[#Stack].ind >= #indent do Stack[#Stack] = nil end
local name, value = line:match'^(.-): (.*)$' name = name or line name = name:match'^(.-) %-' or name -- make names short if value then Stack[#Stack].tab[name] = value else
value = {} Stack[#Stack].tab[name] = value Stack[#Stack+1] = {ind = #indent, tab = value} end end