Thread Tools Display Modes
02-13-11, 06:10 PM   #1
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Checking if a table/saved var is empty.

Is there a way to check if a table or variable is empty? lets say your saved var table has{
["bars"] = {
{
{
["arcLength"] = 360,
["prowl"] = false,
["spellGlow"] = true
}
}
}
}

and i want to check to see if bars is currently an empty table or not.

how would you do that? I tried doing if blahblahvar.bars ~= {} but that only checks to see if its a table or not i want it to check to see if its an empty table.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
02-13-11, 06:26 PM   #2
yssaril
A Warpwood Thunder Caller
 
yssaril's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 96
Code:
if not next(table) then
     -- table is empty
else
     -- table is not empty
end
  Reply With Quote
02-13-11, 06:26 PM   #3
Foxlit
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 91
Code:
function empty(t)
  return next(t) == nil; -- next(t) returns the "first" key in t
end
__________________
... and you do get used to it, after a while.
  Reply With Quote
02-13-11, 09:10 PM   #4
Akkorian
A Flamescale Wyrmkin
 
Akkorian's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 111
Originally Posted by Grimsin View Post
I tried doing if blahblahvar.bars ~= {} but that only checks to see if its a table or not
That will actually only check that “blahblahvar.bars” is not the same table as “{}”... and since you just created that table for your comparison, of course it’s not the same one.

If you wanted to see if “blahblahvar.bars” was a table, you’d do it like this:

Code:
if type( blabhblahvar.bars ) == "table" then ...
__________________
“Be humble, for you are made of earth. Be noble, for you are made of stars.”
  Reply With Quote
02-15-11, 12:38 AM   #5
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
What i needed was the next part the end result was this

if MacaroonSavedState.bars then
if next(MacaroonSavedState.bars) ~= nil then return end
end
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Checking if a table/saved var is empty.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off