在lua中使用for循环创建多个变量

编程入门 行业动态 更新时间:2024-10-28 20:27:38
本文介绍了在lua中使用for循环创建多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

for i = 1,10,1 do marker + i =do things end

几乎我想要得到的是:marker0,marker1,marker2等等。我猜是有问题的标记+我

我得到一个错误。谢谢。

解决方案

你可能不想这样做。更简单的做法是创建一个表格并在表格中创建这些变量作为键。

t = {} for i = 1,10,1 do t [marker.. i] =做事情结束

(请注意, .. 是contuanation,而不是 + in lua。而且你需要引用一个字符串,而不是直接使用它。)

但是,如果你真的希望这些是全局变量而不是键在一些其他的表中,你可以通常(取决于环境)做以下操作: pre $ code>对于i = 1,10,1 do _G [marker.. i] =do things end

I want to use a for loop to create multiple varibles (with names that are the same except for the last character) in lua

for i= 1, 10, 1 do marker+i = "do things" end

pretty much I what I want to get is: marker0, marker1, marker2 and so on. and I guess there is something wrong with marker+i

I get an error. Thank you.

解决方案

You probably don't want to do this actually. Much simpler would be to create a table and create those variables as keys in the table.

t={} for i=1, 10, 1 do t["marker"..i] = "do things" end

(Note that .. is contatenation and not + in lua. Note also that you need to quote a string and not use it literally.)

But if you really want those to be global variables and not keys in some other table you can generally (depending on environment) do the following

for i=1, 10, 1 do _G["marker"..i] = "do things" end

更多推荐

在lua中使用for循环创建多个变量

本文发布于:2023-07-26 07:00:49,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   lua

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!