From 7e18729ff7223a4121eba11fbfbc54f2f01468f7 Mon Sep 17 00:00:00 2001 From: EndMove Date: Fri, 13 Jan 2023 23:03:21 +0100 Subject: [PATCH] begin array... --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ef2d2d3..3f93226 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ Use this table of contents to travel more easily through this cheat sheet. - [Table of contents](#table-of-contents) - [The basics](#the-basics) - [Code comments](#code-comments) - - [Variables and flow control](#variables-and-flow-control) + - [Variables and loop](#variables-and-loop) - [Functions](#functions) - [Advanced](#advanced) - - [Tables/array](#tablesarray) + - [Tables, Array, dict..](#tables-array-dict) - [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. @@ -102,7 +102,7 @@ until num == 0 ### 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 -- The famous Fibonacci sequence. @@ -156,10 +156,57 @@ print 'hello' -- Works fine. ## 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