将字符串拆分为单个字符,然后将这些字符放入表格中 [英] Split a string into individual characters and place the characters into a table

查看:110
本文介绍了将字符串拆分为单个字符,然后将这些字符放入表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,该程序可以将一个单词作为输入,找到单词的每个字母,然后通过我拥有的编码系统运行它.每个字母都分配有一个特定的数字.我希望该程序能够识别字母并将其数字保存到变量中,以便可以通过一些数学运算来运行.我已经完成了数学部分的工作,但一次只能使用一个字符.从我在网上找到的LUA表和字符串库的一些教程来看,我拥有的代码应该可以工作,但是表始终显示为'nil'.

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)

推荐答案

该代码通常可以按您期望的方式工作,但是您在table.insert中输入了错误的表名.应该使用chars而不是t:

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)

这篇关于将字符串拆分为单个字符,然后将这些字符放入表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆