--Script to convert Caled's Affliction table to a lua table.load-compatible format.
--Complicated and a pain in the arse.
--Split into seperate loops for readability
dofile("table.lua")
local f = assert(io.open("input", "r"))
local output = {} --Translated data goes here
local data = ""
print("Opened the input file")
--First replace all "multi word crap" with single;word;stuff:
for line in f:lines() do
--Continue only if 'A' Is the first character f. each line:
if line:find("A", 1) == 1 then
for quotedString in line:gmatch("%b\"\"") do
--Only proceed if there's spaces in the string:
if quotedString:find("[ %-]", 1, true) ~= nil then
local originalString = quotedString
quotedString = quotedString:gsub(" ", ";")
line = line:gsub(originalString, quotedString) --Subbing back into the line
end
end
data = data .. "\n" .. line
end
end
print("Succesfully compressed quoted strings")
--Next remove all quotes (Makes serialisation much easier):
-- Also get rid of that '---' part, simplifying the table serialisation below.
data = data:gsub("\"", "")
data = data:gsub("---", "")
data = data:gsub("A ", "")
print("Replaced unnessescary characters, beginning serialisation")
--[[Lastly sub into the table based on the following reference:
# Name(1) Herb(2) Smoke(3) Salve(4) Fcs(5) Tree(6) Azudim(7) Special(8) Diagnosis(9)
--]]
--Blame lua for not having a switch statement
lookup = {[2]="herb", [3]="smoke", [4]="salve", [5]="focus", [6]="tree", [7]="azudim", [8]="special", [9]="diag"}
print("Created lookup table, writing to output...")
for line in data:gmatch(".-\n") do
--Count var that determines what the word is, should be a better way to do this
local count = 1
local currentAffliction = ""
for word in line:gmatch("[%w/%.;]+") do
--Expand out the ';', now that we're writing to the table:
word = word:gsub(";", " ")
if count == 1 then
currentAffliction = word
output[currentAffliction] = {}
else
-- First check if index should be a boolean (yes/no), if not assume n/a is nil and just assign values
if word == "yes" then
output[currentAffliction][lookup[count]] = true
elseif word == "no" then --Assign as nil, false just means another check when curing
output[currentAffliction][lookup[count]] = nil
elseif word == "n/a" then
output[currentAffliction][lookup[count]] = nil
else
if lookup[count] ~= nil then
output[currentAffliction][lookup[count]] = word
end
end
end
--Increment count
if count > 9 then --This shouldn't happen
count = 1
currentAffliction = nil
else count = count + 1 end
end
end
print("Success!")
--Dump the table
table.save("./output", output)
f:close()