[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua representation of complex tabular data, for use by simulation
- From: Peter Shook <pshook@...>
- Date: Sun, 24 Aug 2003 14:03:58 -0400
Laritz, Thomas wrote:
We are developing a C++ simulation which requires complex tabular (as well as scalar and string) data to be read in at the start of execution. These data are read from files. We'd like to start using Lua syntax to specify these input files. However, given the varied nature of the tabular data, it seems that there might be many ways (some better than others) to represent this tabular data using Lua's capabilities. Can anyone, who has faced this problem, share any techniques, examples or SW?
I find a function name followed by a table record to be the most
flexible approach. Then anyone can process your data in Lua, or store
it in there own C/C++ structures, or store it in a database. The
function can check the record, add a class or metatable, and store it
however and whereever you wish.
I like to store the records in a table as a list. Then it's easy to
grep, map and sort the data.
Here is an example:
$ cat data
-- { style , brand , year, cost, }
rec{ Economy , Ford , 2003, 8.0, }
rec{ Sedan , Ford , 2003, 15.0, }
rec{ Luxury , Ford , 2003, 23.0, }
rec{ Sports , Ford , 2003, 19.0, }
rec{ SUV , Ford , 2003, 26.0, }
rec{ Economy , GM , 2003, 7.0, }
rec{ Sedan , GM , 2003, 19.0, }
rec{ Luxury , GM , 2003, 28.0, }
rec{ Sports , GM , 2003, 24.0, }
rec{ SUV , GM , 2003, 28.0, }
rec{ Economy , Chrysler , 2003, 9.0, }
rec{ Sedan , Chrysler , 2003, 14.0, }
rec{ Luxury , Chrysler , 2003, 25.0, }
rec{ Sports , Chrysler , 2003, 22.0, }
rec{ SUV , Chrysler , 2003, 24.0, }
rec{ Economy , Toyota , 2003, 7.5, }
rec{ Sedan , Toyota , 2003, 17.0, }
rec{ Luxury , Toyota , 2003, 30.0, extra = AC }
rec{ Sports , Toyota , 2003, 28.0, }
rec{ SUV , Toyota , 2003, 31.0, extra = SunRoof }
rec{ Economy , Honda , 2003, 8.2, }
rec{ Sedan , Honda , 2003, 18.0, }
rec{ Luxury , Honda , 2003, 29.0, extra = Leather }
rec{ Sports , Honda , 2003, 32.0, }
rec{ SUV , Honda , 2003, 30.0, extra = AC }
$ cat ex.lua
local key_index = {
style = 1,
brand = 2,
year = 3,
cost = 4,
}
local defaults = {
extra = 'none',
}
local rec_mt = {}
function rec_mt.__index(t, k)
local index = key_index[k]
if index ~= nil then return t[index] end
return assert(defaults[k], 'bad key')
end
all_the_data = {}
local sandbox = {}
function sandbox.rec(rt)
assert(type(rt)=='table', 'record must be a table')
local style, brand, year, cost = unpack(rt)
assert(style, 'bad style')
assert(brand, 'bad brand')
assert(year, 'bad year')
assert(cost, 'bad cost')
setmetatable(rt, rec_mt)
table.insert(all_the_data, rt)
end
local function lit(names)
for n in string.gfind(names, '%w+') do sandbox[n] = n end
end
lit'Economy Sedan Luxury Sports SUV' -- styles
lit'Ford GM Chrysler Toyota Honda' -- brands
lit'AC Leather SunRoof' -- extras
local f = assert(loadfile('data'))
setfenv(f, sandbox)
f()
function rec_mt.__lt(a, b)
return a.cost < b.cost
end
table.sort(all_the_data)
print'sorted by cost'
for i,rt in ipairs(all_the_data) do
io.write(string.format('%-10s %-10s %d %4.1f\n', unpack(rt)))
end
print''
local so = { Economy=1, Sedan=2, Luxury=5, Sports=3, SUV=4 }
local function comp_style_cost(a, b)
local aso, bso = so[a.style], so[b.style]
return aso < bso or (aso == bso and a.cost < b.cost)
end
table.sort(all_the_data, comp_style_cost)
print'sorted by style and cost'
for i,rt in ipairs(all_the_data) do
io.write(string.format('%-10s %4.1f %10s %d %-10s\n',
rt.style, rt.cost, rt.brand, rt.year, rt.extra))
end
$ lua ex.lua
sorted by cost
Economy GM 2003 7.0
Economy Toyota 2003 7.5
Economy Ford 2003 8.0
Economy Honda 2003 8.2
Economy Chrysler 2003 9.0
Sedan Chrysler 2003 14.0
Sedan Ford 2003 15.0
Sedan Toyota 2003 17.0
Sedan Honda 2003 18.0
Sports Ford 2003 19.0
Sedan GM 2003 19.0
Sports Chrysler 2003 22.0
Luxury Ford 2003 23.0
Sports GM 2003 24.0
SUV Chrysler 2003 24.0
Luxury Chrysler 2003 25.0
SUV Ford 2003 26.0
SUV GM 2003 28.0
Sports Toyota 2003 28.0
Luxury GM 2003 28.0
Luxury Honda 2003 29.0
Luxury Toyota 2003 30.0
SUV Honda 2003 30.0
SUV Toyota 2003 31.0
Sports Honda 2003 32.0
sorted by style and cost
Economy 7.0 GM 2003 none
Economy 7.5 Toyota 2003 none
Economy 8.0 Ford 2003 none
Economy 8.2 Honda 2003 none
Economy 9.0 Chrysler 2003 none
Sedan 14.0 Chrysler 2003 none
Sedan 15.0 Ford 2003 none
Sedan 17.0 Toyota 2003 none
Sedan 18.0 Honda 2003 none
Sedan 19.0 GM 2003 none
Sports 19.0 Ford 2003 none
Sports 22.0 Chrysler 2003 none
Sports 24.0 GM 2003 none
Sports 28.0 Toyota 2003 none
Sports 32.0 Honda 2003 none
SUV 24.0 Chrysler 2003 none
SUV 26.0 Ford 2003 none
SUV 28.0 GM 2003 none
SUV 30.0 Honda 2003 AC
SUV 31.0 Toyota 2003 SunRoof
Luxury 23.0 Ford 2003 none
Luxury 25.0 Chrysler 2003 none
Luxury 28.0 GM 2003 none
Luxury 29.0 Honda 2003 Leather
Luxury 30.0 Toyota 2003 AC