Table : Key 1 auto-changed in 2?

Hi.
I have this code :

	total_orbit_chance = {}
	toc = 0
	for i = 1, liste_orbitals[0] do
		toc = toc + liste_orbitals[i]["chance"]
		table.insert (total_orbit_chance, i, toc)
	end
	table.insert (total_orbit_chance, 0, toc)
	pprint (total_orbit_chance)

liste_orbitals[0] is equal to 4, and the liste_orbitals[i][“chance”] are equal to 50,20,15 and 10

the code is supposed to create a total_orbit_chance[0] that are the four others added (95).
That works well
and total_orbit_chance[1 to 4] = 50, 70, 85, 95

But, even if the for loop start at 1, the table.insert seem to create a i+1 key. because the pprint say that :

DEBUG:SCRIPT: 
{ --[[0x7f9372d36640]]
  0 = 95,
  2 = 50,
  3 = 70,
  4 = 85,
  5 = 95
}

Looks like I don’t understand something, but what ? Does the key 0, created AFTER the loop, replace the key 1 ?

Edit : Changed the board from General to Question.

1 Like

I have to admit it’s a bit strange. Non-defold Lua online interpreters crashes with your example.

I tired this code:

total_orbit_chance = {}

liste_orbitals = {{},{},{},{}}
liste_orbitals[0] = 4
liste_orbitals[1]["chance"] = 50
liste_orbitals[2]["chance"] = 20
liste_orbitals[3]["chance"] = 15
liste_orbitals[4]["chance"] = 10

toc = 0
for i = 1, liste_orbitals[0] do
	toc = toc + liste_orbitals[i]["chance"]
	table.insert(total_orbit_chance, i, toc)
end
table.insert(total_orbit_chance, 0, toc)
for k, v in pairs(total_orbit_chance) do
    print(k, v)
end

and it crashed:

lua: main.lua:15: bad argument #2 to 'insert' (position out of bounds)
stack traceback:
	[C]: in function 'table.insert'
	main.lua:15: in main chunk
	[C]: in ?

And it’s make sense. It should crash, because:

" The table.insert function inserts an element in a given position of an array, moving up other elements to open space." – table.insert

Since you don’t have index 0, there is nothing to push to right at index 0.

There are two ways to solve this:
a) Change 0 index to 1.

table.insert(total_orbit_chance, 1, toc)

It will work, however each index will be greater by 1 than what you intend.

b) Don’t use table.insert at all and just use:

total_orbit_chance[0] = toc

However in Defold your code does not crashes, instead I have identical result as you:

{ --[[000001AFF5230560]]
  0 = 95,
  2 = 50,
  3 = 70,
  4 = 85,
  5 = 95
}

Edit: I play’ed a while with this and it seems Defold’s insert works by inserting the whatever index to the table and shifting all indexes greater or equal to inserted to the right, without throwing an error. Try with index -1 for example.

Therefore option b), a.k. changing the line

table.insert(total_orbit_chance, 0, toc)

to:

total_orbit_chance[0] = toc

works perfectly both in Defold and plain Lua.

Since you don’t have index 0, there is nothing to push to right at index 0.

Ooooh… yeah. That’s right : Numeral index 0 don’t really exist in Lua. I probably made the engine sick, and It’s trying to use it as a string in some of its parts, and as a the numeral key in anothers.
Some engines try to do the thing you ask them even when not allowed. Sometimes it’s good, sometimes it’s bad.

I’ll try your ideas. Thanks.

Works, thank you.

We use Lua 5.1.5 (HTML5) or LuaJIT (all other platforms). We do not have a custom table.insert() function.

2 Likes