testing for empty table

Posted by Lethethar on Mon 28 Jul 2014 05:54 PM — 4 posts, 15,524 views.

#0
Run-time error
World: test
Function/Sub: Prompt called by trigger
Reason: processing trigger "Prompt"
[string "Script file"]:6: bad argument #1 to 'next' (table expected, got nil)
stack traceback:
[C]: in function 'next'
[string "Script file"]:6: in function <[string "Script file"]:1>
Error context in script:

this error resulted when testing for empty table with this code

function Prompt (thename, theoutput, wildcards, line)
iHP = wildcards [1]
iMovement = wildcards [2]
iCoins = wildcards [3]
if next(mobs) == nil then
mobsRemaining ()
USA Global Moderator #1
Quote:
[string "Script file"]:6: bad argument #1 to 'next'

next(mobs)

argument #1 to next is mobs
Quote:
(table expected, got nil)

So next must be passed a table. You passed it mobs, which is apparently nil and not a table.
Amended on Mon 28 Jul 2014 06:50 PM by Fiendish
Australia Forum Administrator #2
Maybe test for nil first (ie. no table) and then next (mobs) to see if it is a table, but an empty one.
#3
Thanks, Testing for nil then testing for empty table worked.