From 9c1b2109708aa5e1af5fdc1324be2dec6a0ef71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=BC=80=E7=AE=B1?= Date: Fri, 4 Nov 2022 20:26:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=E6=89=8B=E5=8A=A8=E9=80=89=E9=A2=98][tech?= =?UTF-8?q?]:=2020221104.7=20=E2=AD=90=EF=B8=8F=20How=20to=20iterate=20ove?= =?UTF-8?q?r=20tables=20in=20Lua.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....7 ⭐️ How to iterate over tables in Lua.md | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md diff --git a/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md b/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md new file mode 100644 index 0000000000..4cdc87d0a6 --- /dev/null +++ b/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md @@ -0,0 +1,215 @@ +[#]: subject: "How to iterate over tables in Lua" +[#]: via: "https://opensource.com/article/22/11/iterate-over-tables-lua" +[#]: author: "Seth Kenlon https://opensource.com/users/seth" +[#]: collector: "lkxed" +[#]: translator: " " +[#]: reviewer: " " +[#]: publisher: " " +[#]: url: " " + +How to iterate over tables in Lua +====== + +Create structure that makes it easier to find stored data. + +In the [Lua][1] programming language, an array is called a table. A table is used in Lua to store data. If you're storing a lot of data in a structured way, it's useful to know your options for retrieving that data when you need it. + +### Creating a table in Lua + +To create a table in Lua, you instantiate the table with an arbitrary name: + +``` +mytable ={} +``` + +There are different ways you can structure your data in a table. You could fill it with values, essentially creating a list (called a list in some languages): + +``` +mytable ={'zombie','apocalypse'} +``` + +Or you could create an associated array (called a map or dictionary in some languages). You can add arbitrary keys to the table using dot notation. You can also add a value to that key the same way you add a value to a variable: + +``` +myarray ={} + +myarray.baz='happy' + +myarray.qux='halloween' +``` + +You can add verification with the `assert()` function: + +``` +[assert][2](myarray.baz=='happy','unexpected value in myarray.baz') +[assert][2](myarray.qux=='halloween','unexpected value in myarray.qux') +``` + +You now have two tables: a list-style `mytable` and an associative array-style `myarray`. + +### Iterating over a table with pairs + +Lua's `pairs()` function extracts key and value pairs from a table. + +``` +print('pairs of myarray:') +for k,v in pairs(myarray)do + +  print(k,v) + +end +``` + +Here's the output: + +``` +pairs of myarray: + +baz     happy + +qux     halloween +``` + +If there are no keys in a table, Lua uses an index. For instance, the `mytable` table contains the values `zombie` and `apocalypse`. It contains no keys, but Lua can improvise: + +``` +print('pairs of mytable:') +for k,v in pairs(mytable)do + +  print(k,v) + +end +``` + +Here's the output: + +``` +1   zombie +2   apocalypse +``` + +### Iterating over a table with ipairs + +To account for the fact that tables without keys are common, Lua also provides the `ipairs` function. This function extracts the index and the value: + +``` +print('ipairs of mytable:') +for i,v in ipairs(mytable)do + +  print(i,v) + +end +``` + +The output is, in this case, the same as the output of `pairs`: + +``` +1   zombie +2   apocalypse +``` + +However, watch what happens when you add a key and value pair to `mytable`: + +``` +mytable.surprise='this value has a key' + +print('ipairs of mytable:') +for i,v in ipairs(mytable)do + +  print(i,v) + +end +``` + +Lua ignores the key and value because `ipairs` retrieves only indexed entries: + +``` +1   zombie +2   apocalypse +``` + +The key and value pair, however, have been stored in the table: + +``` +print('pairs of mytable:') +for k,v in ipairs(mytable)do + +  print(k,v) + +end +``` + +The output: + +``` +1          zombie +2          apocalypse + +surprise   this value has a key +``` + +### Retrieving arbitrary values + +You don't have to iterate over a table to get data out of it. You can call arbitrary data by either index or key: + +``` +print('call by index:') + +print(mytable[2]) + +print(mytable[1]) + +print(myarray[2]) + +print(myarray[1]) + +print('call by key:') + +print(myarray['qux']) + +print(myarray['baz']) + +print(mytable['surprise']) +``` + +The output: + +``` +call by index: + +apocalypse + +zombie + +nil + +nil + +call by key: + +halloween + +happy + +this value has a key +``` + +### Data structures + +Sometimes using a Lua table makes a lot more sense than trying to keep track of dozens of individual variables. Once you understand how to structure and retrieve data in a language, you're empowered to generate complex data in an organized and safe way. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/22/11/iterate-over-tables-lua + +作者:[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]: http://www.opengroup.org/onlinepubs/009695399/functions/assert.html From b0ee84c90227fd0fad578881b94b6c203fc2f12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=BC=80=E7=AE=B1?= Date: Fri, 4 Nov 2022 20:43:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=2020221104.7=20=E2=AD=90=EF=B8=8F=20?= =?UTF-8?q?How=20to=20iterate=20over=20tables=20in=20Lua.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....7 ⭐️ How to iterate over tables in Lua.md | 67 +++++-------------- 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md b/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md index 4cdc87d0a6..08bd33cad8 100644 --- a/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md +++ b/sources/tech/20221104.7 ⭐️ How to iterate over tables in Lua.md @@ -19,30 +19,28 @@ In the [Lua][1] programming language, an array is called a table. A table is use To create a table in Lua, you instantiate the table with an arbitrary name: ``` -mytable ={} +mytable = {} ``` There are different ways you can structure your data in a table. You could fill it with values, essentially creating a list (called a list in some languages): ``` -mytable ={'zombie','apocalypse'} +mytable = {'zombie','apocalypse'} ``` Or you could create an associated array (called a map or dictionary in some languages). You can add arbitrary keys to the table using dot notation. You can also add a value to that key the same way you add a value to a variable: ``` -myarray ={} - -myarray.baz='happy' - -myarray.qux='halloween' +myarray = {} +myarray.baz = 'happy' +myarray.qux = 'halloween' ``` You can add verification with the `assert()` function: ``` -[assert][2](myarray.baz=='happy','unexpected value in myarray.baz') -[assert][2](myarray.qux=='halloween','unexpected value in myarray.qux') +[assert][2](myarray.baz == 'happy', 'unexpected value in myarray.baz') +[assert][2](myarray.qux == 'halloween', 'unexpected value in myarray.qux') ``` You now have two tables: a list-style `mytable` and an associative array-style `myarray`. @@ -53,10 +51,8 @@ Lua's `pairs()` function extracts key and value pairs from a table. ``` print('pairs of myarray:') -for k,v in pairs(myarray)do - -  print(k,v) - +for k, v in pairs(myarray) do +  print(k, v) end ``` @@ -64,9 +60,7 @@ Here's the output: ``` pairs of myarray: - baz     happy - qux     halloween ``` @@ -74,10 +68,8 @@ If there are no keys in a table, Lua uses an index. For instance, the `mytable` ``` print('pairs of mytable:') -for k,v in pairs(mytable)do - -  print(k,v) - +for k, v in pairs(mytable) do +  print(k, v) end ``` @@ -94,10 +86,8 @@ To account for the fact that tables without keys are common, Lua also provides t ``` print('ipairs of mytable:') -for i,v in ipairs(mytable)do - -  print(i,v) - +for i, v in ipairs(mytable) do +  print(i, v) end ``` @@ -111,13 +101,10 @@ The output is, in this case, the same as the output of `pairs`: However, watch what happens when you add a key and value pair to `mytable`: ``` -mytable.surprise='this value has a key' - +mytable.surprise = 'this value has a key' print('ipairs of mytable:') -for i,v in ipairs(mytable)do - -  print(i,v) - +for i, v in ipairs(mytable) do +  print(i, v) end ``` @@ -132,10 +119,8 @@ The key and value pair, however, have been stored in the table: ``` print('pairs of mytable:') -for k,v in ipairs(mytable)do - -  print(k,v) - +for k, v in ipairs(mytable) do +  print(k, v) end ``` @@ -144,7 +129,6 @@ The output: ``` 1          zombie 2          apocalypse - surprise   this value has a key ``` @@ -154,21 +138,13 @@ You don't have to iterate over a table to get data out of it. You can call arbit ``` print('call by index:') - print(mytable[2]) - print(mytable[1]) - print(myarray[2]) - print(myarray[1]) - print('call by key:') - print(myarray['qux']) - print(myarray['baz']) - print(mytable['surprise']) ``` @@ -176,21 +152,14 @@ The output: ``` call by index: - apocalypse - zombie - nil - nil call by key: - halloween - happy - this value has a key ```