Split a string into individual characters and place the characters into a table

本秂侑毒 提交于 2019-12-24 05:27:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!