lua-users home
lua-l archive

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


On Sun, Feb 3, 2013 at 8:14 PM, Billy Peacock <spooky_loco_pv@yahoo.com> wrote:
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

assert(Output.Signature == 'AMMO')
assert(Output['Data Size'] == '194')
assert(Output.FormID ==
  'AMMO - Ammunition [0200FF03] <DLC1SoulCairnKeeperArrow> "Dragonbone Arrow"')
assert(Output['Version Control Info 1'] == '1D 71 55 00')
assert(Output['Form Version'] == '43')
assert(Output['Version Control Info 2'] == '04 00')
assert(Output.EDID == 'DLC1SoulCairnKeeperArrow')
assert(Output.X1 == '-2')
assert(Output.Y1 == '-58')
assert(Output.Z1 == '-2')
assert(Output.X2 == '1')
assert(Output.Y2 == '0')
assert(Output.Z2 == '2')
assert(Output.FULL == 'Dragonbone Arrow')
assert(Output.MODL == 'DLC01\\Weapons\\Dragonbone\\Arrow.nif')
assert(Output.MODT == '02 00 00 00 00 00 00 00 00 00 00 00')
assert(Output.YNAM == 'SNDR - Sound Descriptor [0003E7B7] <ITMArrowsUpSD>')
assert(Output.ZNAM == 'SNDR - Sound Descriptor [0003E877] <ITMArrowsDownSD>')
assert(Output.KSIZ == '2')
assert(Output['Keyword #0'] == 'KYWD - Keyword [000917E7] <VendorItemArrow>')
assert(Output['Keyword #1'] ==
  'KYWD - Keyword [0006BBD5] <ArmorMaterialDragonplate>')
assert(Output.Projectile ==
  'PROJ - Projectile [020176F3] <ArrowDragonboneProjectile> "Dragonbone Arrow"')
assert(Output.Flags == 'Non-Playable, Non-Bolt')
assert(Output.Damage == '14.000000')
assert(Output.Value == '4')

-----------------------------------

-- 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

assert(Output.AMMO['Record Header'].Signature == 'AMMO')
assert(Output.AMMO['Record Header']['Data Size'] == '194')
assert(Output.AMMO['Record Header'].FormID ==
  'AMMO - Ammunition [0200FF03] <DLC1SoulCairnKeeperArrow> "Dragonbone Arrow"')
assert(Output.AMMO['Record Header']['Version Control Info 1'] == '1D 71 55 00')
assert(Output.AMMO['Record Header']['Form Version'] == '43')
assert(Output.AMMO['Record Header']['Version Control Info 2'] == '04 00')
assert(Output.AMMO.EDID == 'DLC1SoulCairnKeeperArrow')
assert(Output.AMMO.OBND.X1 == '-2')
assert(Output.AMMO.OBND.Y1 == '-58')
assert(Output.AMMO.OBND.Z1 == '-2')
assert(Output.AMMO.OBND.X2 == '1')
assert(Output.AMMO.OBND.Y2 == '0')
assert(Output.AMMO.OBND.Z2 == '2')
assert(Output.AMMO.FULL == 'Dragonbone Arrow')
assert(Output.AMMO.Model.MODL == 'DLC01\\Weapons\\Dragonbone\\Arrow.nif')
assert(Output.AMMO.Model.MODT == '02 00 00 00 00 00 00 00 00 00 00 00')
assert(Output.AMMO.YNAM == 'SNDR - Sound Descriptor [0003E7B7] <ITMArrowsUpSD>')
assert(Output.AMMO.ZNAM == 'SNDR - Sound Descriptor [0003E877] <ITMArrowsDownSD>')
assert(Output.AMMO.KSIZ == '2')
assert(Output.AMMO.KWDA['Keyword #0'] ==
  'KYWD - Keyword [000917E7] <VendorItemArrow>')
assert(Output.AMMO.KWDA['Keyword #1'] ==
  'KYWD - Keyword [0006BBD5] <ArmorMaterialDragonplate>')
assert(Output.AMMO.DATA.Projectile ==
  'PROJ - Projectile [020176F3] <ArrowDragonboneProjectile> "Dragonbone Arrow"')
assert(Output.AMMO.DATA.Flags == 'Non-Playable, Non-Bolt')
assert(Output.AMMO.DATA.Damage == '14.000000')
assert(Output.AMMO.DATA.Value == '4')


-----------------------------------