translated

This commit is contained in:
geekpi 2022-12-02 09:40:48 +08:00
parent aedeb749cf
commit 97be4259df
2 changed files with 140 additions and 140 deletions

View File

@ -1,140 +0,0 @@
[#]: subject: "Get to know Lua for loops in 4 minutes"
[#]: via: "https://opensource.com/article/22/11/lua-for-loops"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Get to know Lua for loops in 4 minutes
======
In programming, iteration is an important concept because code often must scan over a set of data several times so that it can process each item individually. Control structures enable you to direct the flow of the program based on conditions that are often established dynamically as the program is running. Different languages provide different controls, and in [Lua][1], there's the while loop, for loop, and repeatuntil loop. This article covers for loops. I will cover while and repeat until loops in a separate article.
### For loop
A for loop takes a known quantity of items and ensures that each item is processed. An "item" can be a number. It can also be a table containing several entries or any Lua data type. The syntax and logic are a little flexible, but the syntax allows for these parameters, each of which essentially describes a counter:
- Starting value of the counter
- Stop value
- The increment you want the counter to advance
For instance, suppose you have three items and want Lua to process each. Your counter could start at 3 and last until 1, at an increment of -1. That renders the count of 3, 2, 1.
```
mytable = { "zombie", "Halloween", "apocalypse" }
for count = 3, 1, -1 do
  print(count .. ": " .. mytable[count])
end
```
Run the code to ensure all three items are getting processed:
```
$ lua ./for.lua3: apocalypse2: Halloween1: zombie
```
This code effectively processed the table in "reverse" because it was a countdown. You can count up, instead:
```
for count = 1, 3, 1 do
  print(mytable[count])
end
```
This example processes the table from lowest index to highest:
```
$ lua ./for.lua1: zombie2: Halloween3: apocalypse
```
### Increments
You can change the increment, too. For instance, maybe you want a zombie apocalypse without all the pomp and circumstance of Halloween:
```
mytable = { "zombie", "Halloween", "apocalypse" }
for count = 1, 3, 2 do
  print(mytable[count])
end
```
Run the code:
```
$ lua ./for.lua
zombie
apocalypse
```
The example printed 1 and 3 because the first count was 1, which was then incremented by 2 (for a total of 3).
### Counter
Sometimes you don't know the number of times you need Lua to iterate over data. In this case, you can set your counter to a variable populated by some other process.
Also, the word `count` isn't a keyword. It's just what I'm using in my sample code for clarity. It's common for programmers to use something shorter, such as `i` or `c`.
```
var = os.time()
if var%2 == 0 then
  mytable = { var }
else
  mytable = { "foo", "bar", "baz" }
end
for c = 1, #mytable, 1 do
  print(mytable[c])
end
```
This code creates a variable containing the timestamp of when it was launched. If the timestamp is even (it has a modulo of 0 when divided by 2), then just the timestamp is placed into a table. If the timestamp is odd, it puts three strings into a table.
Now you can't be sure how many times your for loop needs to run. It's either once or thrice, but there's no way to be sure. The solution is to set the starting count to 1 and the final count to the length of the table (`#mytable` is the built-in shortcut to determine the length of a table).
It might take a few times of running the script to see both results, but eventually, you end up with something like this:
```
$ lua ./dynamic.lua1665447960
$ lua ./dynamic.lua
foo
bar
baz
```
### For loops with pairs and ipairs
If you've already read my article on [table iteration][2], then you're already familiar with one of the most common for loops in Lua. This one uses the `pairs` or `ipairs` function to iterate over a table:
```
mytable = { "zombie", "Halloween", "apocalypse" }
for i,v in ipairs(mytable) do
  print(i .. ": " v)
end
```
The `pairs` and `ipairs` functions "unpack" the table and dump the values into the variables you provide. In this example, I use `i` for _index_ and `v` for _value_, but the variables' names don't matter.
```
$ lua ./for.lua1: zombie2: Halloween3: apocalypse
```
### For loop
The for loop structure is common in programming and very common in Lua due to its frequent use of tables and the `pairs` function. Understanding the for loop structure and the options you have when controlling it means you can make clever decisions about how to process data in Lua.
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/11/lua-for-loops
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lkxed
[1]: https://opensource.com/article/22/11/lua-worth-learning
[2]: https://opensource.com/article/22/11/iterate-over-tables-lua

View File

@ -0,0 +1,140 @@
[#]: subject: "Get to know Lua for loops in 4 minutes"
[#]: via: "https://opensource.com/article/22/11/lua-for-loops"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
4 分钟了解 Lua for 循环
======
在编程中,迭代是一个重要的概念,因为代码通常必须多次扫描一组数据,以便它可以单独处理每个项目。控制结构使你能够根据通常在程序运行时动态建立的条件来指导程序的流程。不同的语言提供不同的控制,在 [Lua][1] 中,有 while 循环、for 循环和 repeatuntil 循环。本文介绍 for 循环。我将在另一篇文章中介绍 while 和 repeat until 循环。
### For 循环
for 循环采用已知数量的项目并确保处理每个项目。“项目”可以是数字。它也可以是一个包含多个条目或任何 Lua 数据类型的表。语法和逻辑有点灵活,但语法允许这些参数,每个参数本质上描述了一个计数器:
- 计数器的起始值
- 停止值
- 你希望计数器前进的增量
例如,假设你有三个项目并希望 Lua 处理每个项目。你的计数器可以从 3 开始一直持续到 1增量为 -1。这呈现为 3、2、1 的计数。
```
mytable = { "zombie", "Halloween", "apocalypse" }
for count = 3, 1, -1 do
  print(count .. ": " .. mytable[count])
end
```
运行代码以确保所有三个项目都得到处理:
```
$ lua ./for.lua3: apocalypse2: Halloween1: zombie
```
这段代码有效地“反向”处理了表,因为它是倒数。你可以正数:
```
for count = 1, 3, 1 do
  print(mytable[count])
end
```
此示例从最低索引到最高索引处理表:
```
$ lua ./for.lua1: zombie2: Halloween3: apocalypse
```
### 增量
你也可以更改增量。例如,也许你想要一个没有万圣节盛况的僵尸启示录:
```
mytable = { "zombie", "Halloween", "apocalypse" }
for count = 1, 3, 2 do
  print(mytable[count])
end
```
运行代码:
```
$ lua ./for.lua
zombie
apocalypse
```
该示例打印了 1 和 3因为第一个计数是 1然后递增 2总共 3
### 计数器
有时你不知道需要 Lua 遍历数据的次数。在这种情况下,你可以将计数器设置为由其他进程填充的变量。
另外,`count` 这个词不是关键字。为了清楚起见,这正是我在示例代码中使用的内容。程序员通常使用更短的名称,例如 `i``c`
```
var = os.time()
if var%2 == 0 then
  mytable = { var }
else
  mytable = { "foo", "bar", "baz" }
end
for c = 1, #mytable, 1 do
  print(mytable[c])
end
```
此代码创建一个变量,其中包含启动时的时间戳。如果时间戳是偶数(除以 2 时模数为 0则只将时间戳放入表中。如果时间戳是奇数它将三个字符串放入一个表中。
现在你无法确定您的 for 循环需要运行多少次。可能是一次或是三次,但没有办法确定。解决方案是将起始计数设置为 1将最终计数设置为表的长度`#mytable` 是确定表长度的内置快捷方式)。
可能需要多次运行脚本才能看到这两个结果,但最终,你会得到如下结果:
```
$ lua ./dynamic.lua1665447960
$ lua ./dynamic.lua
foo
bar
baz
```
### 带 pairs 和 ipairs 的 for 循环
如果你已经阅读了我关于[表迭代][2]的文章,那么你已经熟悉了 Lua 中最常见的 for 循环之一。这个使用 `pairs``ipairs` 函数来迭代一个表:
```
mytable = { "zombie", "Halloween", "apocalypse" }
for i,v in ipairs(mytable) do
  print(i .. ": " v)
end
```
`pairs``ipairs` 函数“解包”表并将值转储到你提供的变量中。在此示例中,我将 `i` 用于 _index_,将 `v` 用于 _value_,但变量名称无关紧要。
```
$ lua ./for.lua1: zombie2: Halloween3: apocalypse
```
### For 循环
for 循环结构在编程中很常见,由于经常使用表和 pairs 函数,因此在 Lua 中非常常见。了解 for 循环结构和控制它时的选项意味着你可以就如何在 Lua 中处理数据做出明智的决定。
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/11/lua-for-loops
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lkxed
[1]: https://opensource.com/article/22/11/lua-worth-learning
[2]: https://opensource.com/article/22/11/iterate-over-tables-lua