Lua 上的 For 循环 [英] For Loop on Lua

查看:29
本文介绍了Lua 上的 For 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是如何做一个 for 循环.我已经根据数字弄清楚了,但无法根据名称弄清楚.我想创建一个运行名称列表的 for 循环.以下是我到目前为止所拥有的:

My assignment is how to do a for loop. I have figured it out in terms of numbers but cannot figure it out in terms of names. I would like to create a for loop that runs down a list of names. Following is what I have so far:

names = {'John', 'Joe', 'Steve'}
for names = 1, 3 do
  print (names)
end

我尝试了其他一些东西,但它不起作用,终端总是只列出 1、2、3……我做错了什么?

I have tried some other things but it just doesn't work, the terminal always just lists 1, 2, 3... What I am doing wrong?

推荐答案

你的问题很简单:

names = {'John', 'Joe', 'Steve'}
for names = 1, 3 do
  print (names)
end

这段代码首先声明了一个名为names的全局变量.然后,您开始一个 for 循环.for 循环声明了一个 local 变量,它恰好也被称为 names;变量先前已用 names 定义的事实完全无关紧要.在 for 循环中使用 names 将引用本地,而不是全局.

This code first declares a global variable called names. Then, you start a for loop. The for loop declares a local variable that just happens to be called names too; the fact that a variable had previously been defined with names is entirely irrelevant. Any use of names inside the for loop will refer to the local one, not the global one.

for 循环表示循环的内部部分将使用 names = 1,然后 names = 2,最后 names = 3.for 循环声明了一个从第一个数字到最后一个数字的计数器,并且它会为它计数的每个值调用一次内部代码.

The for loop says that the inner part of the loop will be called with names = 1, then names = 2, and finally names = 3. The for loop declares a counter that counts from the first number to the last, and it will call the inner code once for each value it counts.

你真正想要的是这样的:

What you actually wanted was something like this:

names = {'John', 'Joe', 'Steve'}
for nameCount = 1, 3 do
  print (names[nameCount])
end

[] 语法是您访问 Lua 表成员的方式.Lua 表将键"映射到值".您的数组会自动创建整数类型的键,这些键会增加.所以表中与Joe"相关的键是2(Lua索引总是从1开始).

The [] syntax is how you access the members of a Lua table. Lua tables map "keys" to "values". Your array automatically creates keys of integer type, which increase. So the key associated with "Joe" in the table is 2 (Lua indices always start at 1).

因此,您需要一个从 1 到 3 计数的 for 循环,这是您得到的.您可以使用 count 变量来访问表中的元素.

Therefore, you need a for loop that counts from 1 to 3, which you get. You use the count variable to access the element from the table.

然而,这有一个缺陷.如果从列表中删除其中一个元素会发生什么?

However, this has a flaw. What happens if you remove one of the elements from the list?

names = {'John', 'Joe'}
for nameCount = 1, 3 do
  print (names[nameCount])
end

现在,我们得到 John Joe nil,因为尝试从不存在的表中访问值会导致 nil.为了防止这种情况,我们需要从 1 数到表的长度:

Now, we get John Joe nil, because attempting to access values from a table that don't exist results in nil. To prevent this, we need to count from 1 to the length of the table:

names = {'John', 'Joe'}
for nameCount = 1, #names do
  print (names[nameCount])
end

# 是长度运算符.它适用于表和字符串,返回两者的长度.现在,无论 names 有多大或多小,这都将始终有效.

The # is the length operator. It works on tables and strings, returning the length of either. Now, no matter how large or small names gets, this will always work.

但是,有一种更方便的方法来遍历项目数组:

However, there is a more convenient way to iterate through an array of items:

names = {'John', 'Joe', 'Steve'}
for i, name in ipairs(names) do
  print (name)
end

ipairs 是一个迭代列表的 Lua 标准函数.这种for循环的风格,for循环的迭代器,就是使用了这种迭代器函数.i 值是数组中条目的索引.name 值是该索引处的值.所以它基本上为你做了很多繁重的工作.

ipairs is a Lua standard function that iterates over a list. This style of for loop, the iterator for loop, uses this kind of iterator function. The i value is the index of the entry in the array. The name value is the value at that index. So it basically does a lot of grunt work for you.

这篇关于Lua 上的 For 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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