table.foreach is behaving strangely
Cyakat opened this issue ยท 1 comments
Minecraft Version
1.20.x
Version
1.108.3
Details
This is in forge version 47.1.3
I am also using Javav 17.0.1 if that is any help
I have created a table with 3 key-value pairs and the table.foreach
function is not showing all of them when I run the function with print
I have tested this with normal Lua and it seems to work just fine
t = {value1 = 1, value2 = 2, value3 = 3}
table.foreach(t, print)
should return this
value2 2
value3 3
value1 1
however it returns this instead
value2 2
1
It still happens on a single player world with CC and jei as the only mods.
This also still happens on the latest version of forge (47.2.1)
This is due the the fact that print
returns a value under CC (the number of lines printed). This causes table.foreach
to immediately exit the loop.
You can work around this by eta-expanding print to function(...) print(...) end
.
However, I'd strongly recommend using a normal for loop here instead - table.foreach
is deprecatod and removed in future versions of Lua. While it's unlikely we'll ever remove it, it's definitely a bit of a smell.