recursion/Tail Calls problem in Corona

Hello,

I’m trying to write a function for path finding.

--//node table is like this: --//{{y, x, cost, parent}, {y2, x2, cost2, parent2}} --//A function like this: function count(nodeTable) local y, x, cost, parent for i=1, #nodeTabledo y = nodeTable[i][1] --// ALWAYS error here if #nodeTable is not 1 when I try tail calls x = nodeTable[i][2] cost = nodeTable[i][3] parent = nodeTable[i][4] doSomething() --//checks things, removes checked nodes and insert new nodes into a new table for the next loop end if(#newNodeTable ~= 0) then return count(newNodeTable) --// tail call end end

However it will cause a error when the size of node table is not 1 at the line I marked

If I don’t use tail call, that line will pass just fine, but I need recursion t finish the path finder.

There must be something wrong. :frowning:

Can someone help me?

Thanks!
                

 

Not sure if this is the problem, or at least part of the problem … #newNodeTable always returns 0 for hash-tables.  This has something to do with how Lua stores tables and arrays (the # operator counts entries until it hits a nil … that could be anywhere in a hash-table).

Google’ing for ‘lua size of hash table’ suggests that you just count the entries with your own loop:

numItems = 0 for k,v in pairs(myTable) do numItems = numItems + 1 end

Not sure if this is the problem, or at least part of the problem … #newNodeTable always returns 0 for hash-tables.  This has something to do with how Lua stores tables and arrays (the # operator counts entries until it hits a nil … that could be anywhere in a hash-table).

Google’ing for ‘lua size of hash table’ suggests that you just count the entries with your own loop:

numItems = 0 for k,v in pairs(myTable) do numItems = numItems + 1 end