Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Message
| Well, you can import the data into a SQLite3 memory database, but sticking with tables, we can have fun playing with them ...
To analyze by item (rather than by store) we probably need the items at the top level rather than stores, so we can more easily sort and compare.
One approach is to simply store the data differently, eg.
goods = goods or {}
goods ["%1"] = goods ["%1"] or {}
goods ["%1"] ["@cur_village"] = { buy = %2, stock = %3, sell = %4 }
That way the highest table is goods (eg. wood, silver) and the next level down is where you found them.
But assuming you already have your data, you can convert the table easily enough:
-- test data
stores = {
Acknor = {
Wood = {
sell = 68,
stock = 0,
buy = 8,
},
Silver = {
sell = 118,
stock = 0,
buy = 14,
},
},
Paavik = {
Wood = {
sell = 68,
stock = 104,
buy = 5,
},
Silver = {
sell = 119,
stock = 0,
buy = 5,
},
},
}
-- convert table into one with the goods at the top level
goods = {}
for store, inventory in pairs (stores) do
for item, details in pairs (inventory) do
goods [item] = goods [item] or {} -- make sure this item exists
goods [item] [store] = details
end -- for each item
end -- for each store
-- show results
print (string.rep ("-", 60))
tprint (goods)
print (string.rep ("-", 60))
-- get goods in alphabetic order
goods_alpha = {}
for k in pairs (goods) do
table.insert (goods_alpha, k)
end -- for
table.sort (goods_alpha)
-- show each item
for _, item in ipairs (goods_alpha) do
print ""
print (item)
print (string.rep ("-", 60))
-- stores that stock this item
local item_store = {}
for k in pairs (goods [item]) do
table.insert (item_store, k)
end -- for
table.sort (item_store, function (a, b)
return goods [item] [a].buy < goods [item] [b].buy
end)
for _, store in ipairs (item_store) do
print (store,
"Buy", goods [item] [store].buy,
"Sell", goods [item] [store].sell,
"Stock", goods [item] [store].stock)
end -- for
end -- for
Output from the above:
------------------------------------------------------------
"Wood":
"Acknor":
"sell"=68
"stock"=0
"buy"=8
"Paavik":
"sell"=68
"stock"=104
"buy"=5
"Silver":
"Acknor":
"sell"=118
"stock"=0
"buy"=14
"Paavik":
"sell"=119
"stock"=0
"buy"=5
------------------------------------------------------------
Silver
------------------------------------------------------------
Paavik Buy 5 Sell 119 Stock 0
Acknor Buy 14 Sell 118 Stock 0
Wood
------------------------------------------------------------
Paavik Buy 5 Sell 68 Stock 104
Acknor Buy 8 Sell 68 Stock 0
Once I converted the table into having goods at the top, followed by villages, I made another temporary table (goods_alpha) which let me sort them into alpha order (so silver comes before wood).
Then iterating through that table with ipairs, for each item we then make yet another temporary table for each store that sells that item (item_store).
A sort with a custom compare function then sorts those items into buy order (change the word "buy" in two places to sort into sell or stock order).
Then we print the results.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|