begin array...

This commit is contained in:
Jérémi N ‘EndMove’ 2023-01-13 23:03:21 +01:00
parent 336cb76e73
commit 7e18729ff7
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4

View File

@ -10,10 +10,10 @@ Use this table of contents to travel more easily through this cheat sheet.
- [Table of contents](#table-of-contents) - [Table of contents](#table-of-contents)
- [The basics](#the-basics) - [The basics](#the-basics)
- [Code comments](#code-comments) - [Code comments](#code-comments)
- [Variables and flow control](#variables-and-flow-control) - [Variables and loop](#variables-and-loop)
- [Functions](#functions) - [Functions](#functions)
- [Advanced](#advanced) - [Advanced](#advanced)
- [Tables/array](#tablesarray) - [Tables, Array, dict..](#tables-array-dict)
- [Metatables and metamethods](#metatables-and-metamethods) - [Metatables and metamethods](#metatables-and-metamethods)
- [](#) - [](#)
@ -30,7 +30,7 @@ Use this table of contents to travel more easily through this cheat sheet.
--]] --]]
```` ````
### Variables and flow control ### Variables and loop
Introduction to variables, basic conditions, some loops ([examples available here](support/while.lua)) and the equivalent of the ternary operator. Introduction to variables, basic conditions, some loops ([examples available here](support/while.lua)) and the equivalent of the ternary operator.
@ -102,7 +102,7 @@ until num == 0
### Functions ### Functions
Abordons la définition de fonctions plus complètes, la récurcivité, les closures. Découverte de l'asignation d'une suite de valeur a une suite de variable. Introduction to function definition, recursion with lua and chain assignment followed by closure function.
````lua ````lua
-- The famous Fibonacci sequence. -- The famous Fibonacci sequence.
@ -156,10 +156,57 @@ print 'hello' -- Works fine.
## Advanced ## Advanced
### Tables/array ### Tables, Array, dict..
```` ````lua
-- Tables = Lua's only compound data structure;
-- they are associative arrays.
-- Similar to php arrays or js objects, they are
-- hash-lookup dicts that can also be used as lists.
-- Using tables as dictionaries / maps:
-- Dict literals have string keys by default:
t = {key1 = 'value1', key2 = false}
-- String keys can use js-like dot notation:
print(t.key1) -- Prints 'value1'.
t.newKey = {} -- Adds a new key/value pair.
t.key2 = nil -- Removes key2 from the table.
-- Literal notation for any (non-nil) value as key:
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28]) -- prints "tau"
-- Key matching is basically by value for numbers
-- and strings, but by identity for tables.
a = u['@!#'] -- Now a = 'qbert'.
b = u[{}] -- We might expect 1729, but it's nil:
-- b = nil since the lookup fails. It fails
-- because the key we used is not the same object
-- as the one used to store the original value. So
-- strings & numbers are more portable keys.
-- A one-table-param function call needs no parens:
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'.
for key, val in pairs(u) do -- Table iteration.
print(key, val)
end
-- _G is a special table of all globals.
print(_G['_G'] == _G) -- Prints 'true'.
-- Using tables as lists / arrays:
-- List literals implicitly set up int keys:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do -- #v is the size of v for lists.
print(v[i]) -- Indices start at 1 !! SO CRAZY !!
end
-- A 'list' is not a real type. v is just a table
-- with consecutive integer keys, treated as a list.
```` ````
#### Metatables and metamethods #### Metatables and metamethods