Absolutly dumb problem with tables.

Why is this line of code:

table.remove(tmpArray,idx)

generating this error:

…/loading.lua:31: attempt to call field ‘remove’ (a nil value)

I’m sure I’m missing something super simple. [import]uid: 19626 topic_id: 16359 reply_id: 316359[/import]

I played around a bit with and without seeing more code did you perhaps assign your own local variable of “table” to something? If so, that would give you this error you are seeing as opposed to using the Lua table functions.
As an example…

tmpArray = {}  
idx = 0  
  
table = tmpArray  
table.remove(tmpArray,idx)  
  
print("nice")  

If you have done something like line 4 in your code you will get the error you are seeing, comment out line 4 and it works as you would expect.

Hopefully that helps give you a clue…good luck!

–Croisened

[import]uid: 48203 topic_id: 16359 reply_id: 61003[/import]

DOH!!! (In my best Homer Simpson).

function shuffleTable(table)  
 local tmpArray = {}  
 local shuffledArray = {}  
 local i  
 for i = 1, #table do  
 tmpArray[i] = table[i]  
 end  
 local entries = #table  
 math.randomseed(os.time()) -- reseed the random number generator  
 local random = math.random  
  
 for i = 1, entries do  
 local idx = random(#tmpArray)  
 shuffledArray[i] = tmpArray[idx]  
 table.remove(tmpArray,idx)  
 end  
 return shuffledArray  
end  

Notice I’m passing a variable to my shuffle routine and I’m naming it “table” which happens to be overwriting the API table definition, ergo no .remove() method!!!

DOH!!!
[import]uid: 19626 topic_id: 16359 reply_id: 61008[/import]