lua - How do I make my add command find the value of the first variable and the second then add them together? -
vars = {} values = {} function open(file) lex(file) end function lex(file) local data = io.open(file, "r") char in data:lines() --print if char:sub(1, 6) == "print:" print(char:sub(7)) end --integer if char:sub(1, 2) == "v:" vars [#vars + 1] = char:sub(3, 5) if char:sub(6, 6) == "=" values [#values + 1] = char:sub(7) end --print("name:"..vars [#vars]..", ".."value:"..values [#values]) end --add if char:sub(1, 4) == "add:" if char:sub(5, 7) == vars[#vars] if char:sub(8, 8) == "," if char:sub(9, 11) == vars[#vars] print(values[#values] + values[#values]) --print(vars[#vars]) end end end end end --debug purposes --[[ k, v in pairs(vars) print(k, v) end b, in pairs(values) print(b, a) end --]] end function run() while true print("open file") file = io.read() print("file name:"..file) print("") lex(file) print("") end end run()
basically says "--add" want variable set name wrote down in text file the other set same name example: v:var=12 (nextline) v:vr2=10 (nextline) add:var,vr2, add values set it
you're on right track. changes make put comes after v: key in vars table, , set comes after = value, so:
if char:sub(1,2) == "v:" , char:sub(6, 6) == "=" vars[char:sub(3,5)] = tonumber(char:sub(7)) end
then, have add function this:
function add_vars(one, two) return vars[one] + vars[two] end
at least, think understand you're wanting. if it's not, you'll want go more detail getting , you're expecting.
Comments
Post a Comment