Share Pastebin
Guest
Public paste!

Lycaon

By: a guest | Jun 30th, 2009 | Syntax: Lua | Size: 2.88 KB | Hits: 19 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. --Script to convert Caled's Affliction table to a lua table.load-compatible format.
  2. --Complicated and a pain in the arse.
  3.  
  4. --Split into seperate loops for readability
  5. dofile("table.lua")
  6.  
  7. local f = assert(io.open("input", "r"))
  8. local output = {} --Translated data goes here
  9. local data = ""
  10.  
  11. print("Opened the input file")
  12.  
  13. --First replace all "multi word crap" with single;word;stuff:
  14. for line in f:lines() do
  15.   --Continue only if 'A' Is the first character f. each line:
  16.   if line:find("A", 1) == 1 then
  17.     for quotedString in line:gmatch("%b\"\"") do
  18.       --Only proceed if there's spaces in the string:
  19.         --Oops, forgot to enable regexp's
  20.       if quotedString:find("[ %-]") ~= nil then
  21.         local originalString = quotedString
  22.  
  23.         quotedString = quotedString:gsub(" ", ";")
  24.         line = line:gsub(originalString, quotedString) --Subbing back into the line
  25.       end
  26.     end
  27.     data = data .. "\n" .. line
  28.   end
  29. end
  30.  
  31. print("Succesfully compressed quoted strings")
  32.  
  33. --Next remove all quotes (Makes serialisation much easier):
  34. -- Also get rid of that '---' part, simplifying the table serialisation below.
  35. data = data:gsub("\"", "")
  36. data = data:gsub("---", "")
  37. data = data:gsub("A ", "")
  38. print("Replaced unnessescary characters, beginning serialisation")
  39.  
  40. --[[Lastly sub into the table based on the following reference:
  41.     # Name(1)                 Herb(2)       Smoke(3)    Salve(4)       Fcs(5) Tree(6) Azudim(7)  Special(8)     Diagnosis(9)
  42. --]]
  43.  
  44. --Blame lua for not having a switch statement
  45. lookup = {[2]="herb", [3]="smoke", [4]="salve", [5]="focus", [6]="tree", [7]="azudim", [8]="special", [9]="diag"}
  46. print("Created lookup table, writing to output...")
  47. for line in data:gmatch(".-\n") do
  48.   --Count var that determines what the word is, should be a better way to do this
  49.   local count = 1
  50.   local currentAffliction = ""
  51.   for word in line:gmatch("[%w/%.;]+") do
  52.     --Expand out the ';', now that we're writing to the table:
  53.     word = word:gsub(";", " ")
  54.     if count == 1 then
  55.       currentAffliction = word
  56.       output[currentAffliction] = {}
  57.     else
  58.      -- First check if index should be a boolean (yes/no), if not assume n/a is nil and just assign values
  59.       if word == "yes" then
  60.         output[currentAffliction][lookup[count]] = true
  61.       elseif word == "no" then --Assign as nil, false just means another check when curing
  62.         output[currentAffliction][lookup[count]] = nil
  63.       elseif word == "n/a" then
  64.         output[currentAffliction][lookup[count]] = nil
  65.       else
  66.         if lookup[count] ~= nil then
  67.           output[currentAffliction][lookup[count]] = word
  68.         end
  69.       end
  70.     end
  71.     --Increment count
  72.     if count > 9 then --This shouldn't happen
  73.       count = 1
  74.       currentAffliction = nil
  75.     else count = count + 1 end
  76.   end
  77. end
  78. print("Success!")
  79.  
  80. --Dump the table
  81. table.save("./output", output)
  82. f:close()