Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function values(v) -- general diy iterator
- local i = 0
- return function()
- i = i + 1
- return v[i]
- end
- end
- function printColours()
- local colours = {"red","blue","green"} --lua table acting as a list
- for colour in values(colours) do -- call the iterator function
- print(colour)
- end
- end
- function printCommands()
- local go = "U3 L6 R5 F15 B3 D2"
- print("Original go:"..go)
- go = string.gsub(go, " ", "") -- remove all spaces and substitute with empty string ""
- print("Stripped go:"..go)
- local commandList = {}
- local command = ""
- -- make a list of commands from go string eg "U3L6R5F15B3D2" = {"U3", "L6", "R5", "F15", "B3", "D2"}
- for i = 1, string.len(go) do
- local character = string.sub(go, i, i) -- examine each character in the string
- if tonumber(character) == nil then -- char is NOT a number
- if command ~= "" then -- command is NOT empty eg "x0"
- table.insert(commandList, command) -- add to table eg "x0"
- end
- command = character -- replace command with new character eg "F"
- else -- char IS a number
- command = command..character -- add eg 1 to F = F1, 5 to F1 = F15
- if i == string.len(go) then -- last character in the string
- table.insert(commandList, command)
- end
- end
- end
- for command in values(commandList) do -- call the iterator function
- print(command)
- end
- end
- function main()
- --[[
- Python:
- colours = ["red", "blue", "green"]
- for colour in colours:
- print(colour)
- ]]
- --lua has to have a generic iterator to do the same thing:
- printColours()
- printCommands()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement