From b3189cddcda5a4ada4e47cc7fccf411aaf9fde90 Mon Sep 17 00:00:00 2001 From: "Puschnig Jason Leander (DES DOS IP AMS CM ADC)" Date: Thu, 14 Aug 2025 14:38:28 +0200 Subject: [PATCH 1/3] add explanation for pairs vs. ipairs --- lua.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lua.md b/lua.md index 03b6249148..a7db7b5235 100644 --- a/lua.md +++ b/lua.md @@ -167,7 +167,8 @@ b = u[{}] -- We might expect 1729, but it's nil: function h(x) print(x.key1) end h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'. -for key, val in pairs(u) do -- Table iteration. +-- caution: the ordering here is undefined! +for key, val in pairs(u) do -- Table iteration print(key, val) end @@ -181,6 +182,12 @@ 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 + +-- Alternatively: +-- ipairs() guarantees the same order (unlike pairs) +for i, val in ipairs(v): + print("v[", i, "]=", val) + -- A 'list' is not a real type. v is just a table -- with consecutive integer keys, treated as a list. From 8f85576d0832f6a65d2449ff838fec742b48faf4 Mon Sep 17 00:00:00 2001 From: "Puschnig Jason Leander (DES DOS IP AMS CM ADC)" Date: Thu, 14 Aug 2025 14:43:51 +0200 Subject: [PATCH 2/3] minor clarification --- lua.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.md b/lua.md index a7db7b5235..b05e7cc596 100644 --- a/lua.md +++ b/lua.md @@ -184,7 +184,7 @@ for i = 1, #v do -- #v is the size of v for lists. end -- Alternatively: --- ipairs() guarantees the same order (unlike pairs) +-- ipairs() guarantees the same order every time for i, val in ipairs(v): print("v[", i, "]=", val) From 87c37ea8e90d8869fb6cfb452e1644a1f394cced Mon Sep 17 00:00:00 2001 From: "Puschnig Jason Leander (DES DOS IP AMS CM ADC)" Date: Thu, 14 Aug 2025 14:49:53 +0200 Subject: [PATCH 3/3] favor string concat to produce a better output --- lua.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.md b/lua.md index b05e7cc596..2a6efabd0a 100644 --- a/lua.md +++ b/lua.md @@ -186,7 +186,7 @@ end -- Alternatively: -- ipairs() guarantees the same order every time for i, val in ipairs(v): - print("v[", i, "]=", val) + print("v[" .. i .. "] = " .. val) -- A 'list' is not a real type. v is just a table -- with consecutive integer keys, treated as a list.