问题
I'm trying to make a program that can take a word as an input, find each letter of the word and then run it through an encoding system I have. Each letter is assigned to a specific number. I'd like the program to be able to identify a letter and save it's number into a variable so it can be run through a few math operations. I have the math portion done and working, but it can only take one character at a time. Judging from some tutorials I've found online for the LUA tables and strings library, the code I have should work, but the table always prints 'nil'.
print("Word?")
str = io.read()
chars = {}
for i in string.gmatch(str, "%U") do
table.insert(t, i)
end
print(chars)
回答1:
The code mostly working as you expect it, but you mistyped the table name in table.insert
; it should be using chars
instead of t
:
table.insert(chars, i)
来源:https://stackoverflow.com/questions/26167239/split-a-string-into-individual-characters-and-place-the-characters-into-a-table