well, its kinda simple and straight forward,
i just want to know the best method of checking if theres nothing in a table.
i used to use "if table==nil then ..."
but currently it doesnt seem to work!!
if table==nil means the table does not exist
if table=={} means table exists but empty
Hope this works 4 u.
That should help you a lil bit more. .
--- for an associative table, like ["smth"] = "smth else",
function isEmpty(t)
for i,v in t do
return false;
end
return true;
end;
--- for an array ( a table with only numerical indexes ), like : [1] = "smth" or { "smth", "smth" }
if table.getn(t) == 0 then
So far I thought empty tables and empty arrays have the same value: {}. Was I wrong? :D
QuoteOriginally posted by bastya_elvtars
So far I thought empty tables and empty arrays have the same value: {}. Was I wrong? :D
Arrays are represented by tables in LUA, so array is the same as table (just to make sure no-one gets confused ;-)
Theoretically it's possible to check if a table is equal to {}, but that isn't entirely right. Correct LUA tables (note: most people don't make correct tables ;-)) should contain an [n] field, which contains a number of objects in the table - so even an empty table ain't that empty - you should always use this to make sure the table exists and contains at least one object:
if my_tab and (table.getn(my_tab) > 0) then...
table.getn(my_tab) in fact returns the my_tab[n] or my_tab.n value, but definitely prefer using the function
[n] field is represented by table.getn, and is only important on table.setn, but I may be wrong. But does table.getn work for associative tables too? Can this table be getn-ed?
local commandtable={
["invite"]= {invite,{user,data,env,"temp"}},
["os.remove"]={os.removeopchat,{user,data,env}},
["perminvite"]={invite,{user,data,env,"perm"}},
["immune"]={immune,{user,data,env}},
["delimmune"]={delimmune,{user,data,env}},
["showimmuned"]={showimmuned,{user,env}},
["report"]={report,{user,data,env}},
["slothublimit"]={showhubslotrules,{user,env}},
["showops"]={readregusers,{user,1,"operators",levshowops,env}},
["showsuperops"]={readregusers,{user,0,"superoperators",levshowsuperops,env}},
["showvips"]={readregusers,{user,2,"VIPs",levshowvips,env}},
["showregusers"]={readregusers,{user,3,"registered users",levshowregusers,env}},
["warn"]={dowarn,{user,data,env}},
["resetwarn"]={resetwarn,{user,data,env}},
["mute"]={DoMute,{user, data,env}},
["unmute"]={DoUnMute,{user, data,env}},
["showmuted"]={ShowMuted,{user,env}},
["gag"]={DoMute,{user, data,env}},
["ungag"]={DoUnMute,{user, data,env}},
["addop"]={register,{user,"op",data,levaddop,env}},
["addvip"]={register,{user,"vip",data,levaddvip,env}},
["addsuperop"]={register,{user,"superop",data,levaddsuperop,env}},
["addreg"]={register,{user,"reg",data,levaddreg,env}},
["deleteop"]={unregister,{user,"op",data,levaddop,env}},
["deletevip"]={unregister,{user,"vip",data,levdeletevip,env}},
["deletesuperop"]={unregister,{user,"superop",data,levdeletesuperop,env}},
["deletereg"]={unregister,{user,"reg",data,levdeletereg,env}},
["chpass"]={chpass,{user,data,env}},
["addkick"]={addrandomkick,{user,data,env}},
["delkick"]={delrandomkick,{user,data,env}},
["randomkick"]={setrandomkick,{user,data,env}},
["showrandomkicks"]={showrandomkicks,{user,env}},
["kickuser"]={Kicking,{user,data,env}},
["kick"]={Kicking,{user,data,env}},
["nuke"]={nuke,{user,data,env}},
["ban"]={nuke,{user,data,env}},
["tban"]={tban,{user,data,env}},
["op"]={operror,{user,env}},
["rangeban"]={rangeban,{user,data,env}},
["rangeunban"]={rangeunban,{user,data,env}},
-- messaging thingies
["banner"]={banner,{user,data,env}}, -- thx Nathanos
["mass"]={massmsg,{user,data,env}},
["say"]={say,{user,data,env}},
["pmops"]={opm,{user,data,env}},
["botpm"]={botpm,{user,data,env}},
-- custom textreading command thingies
[customcmd1]={showtextfile,{user,customtext1,customcmdlev1,env}},
[customcmd2]={showtextfile,{user,customtext2,customcmdlev2,env}},
[customcmd3]={showtextfile,{user,customtext3,customcmdlev3,env}},
[customcmd4]={showtextfile,{user,customtext4,customcmdlev4,env}},
[customcmd5]={showtextfile,{user,customtext5,customcmdlev5,env}},
[relcmd1]={addrelease,{user,data,env}},
[relcmd2]={showreleases,{user,data,env}},
[relcmd3]={delrelease,{user,data,env}},
[relcmd4]={reloadreleases,{user,env}},
---------------- custom add/show/del command shit
[gbcmd1]={showshit,{user,gbname,gbfile,levgbcmd1,env}},
[gbcmd2]={addshit,{user,data,gbfile,levgbcmd2,env}},
[gbcmd3]={delshit,{user,data,gbfile,gbname,levgbcmd3,env}},
-- news stuff
[ncmd1]={showshit,{user,nname,nfile,levncmd1,env}},
[ncmd2]={addshit,{user,data,nfile,levncmd2,env}}, -- ,{user,data,container,file}},
[ncmd3]={delshit,{user,data,nfile,nname,levncmd3,env}}, -- delshit,{user,data,container,file,wtf}},
-- requests stuff
[rcmd1]={showshit,{user,rname,rfile,levrcmd1,env}}, -- ,{user,container,wtf,file}},
[rcmd2]={addshit,{user,data,rfile,levrcmd2,env}}, -- ,{user,data,container,file}},
[rcmd3]={delshit,{user,data,rfile,rname,levrcmd3,env}}, -- delshit,{user,data,container,file,wtf}},
-- hub setting/info gathering stuff
["sethubdesc"]={sethubdesc,{user,data,env}},
["sethubname"]={sethubname,{user,data,env}},
["redirectall"]={RedirectAllNow,{user,data,env}},
["chred"]={ChRed,{user,data,env}},
["setredirectall"]={SetRedirectAll,{user,data,env}},
["setredirectfull"]={SetRedirectFull,{user,data,env}},
["setmaxusers"]={SetMaxUsers,{user, data,env}},
["redirectuser"]={redirectuser,{user,data,env}},
["help"]={help,{user}},
["myip"]={myip,{user,env}},
["me" ]={me,{user,data,env}},
["uptime"]={showuptime,{user,env}},
["hubstats"]={showhubstats,{user,env}},
["afk"]={away,{user,data,env}},
["away"]={away,{user,data,env}},
["back"]={returned,{user,env}},
["amiaway"]={myawaystatus,{user,env}},
["drop"]={droperror,{user,env}},
["showbanlist"]={showbanlist,{user,env}},
["showbannedranges"]={showbannedranges,{user,env}},
["showaway"]={showawaymsgs,{user,data,env}},
["hubinfo"]={hubinfo,{user,env}},
["userinfo"]={infoonuser,{user,data,env}},
["ver"]={botversion,{user,env,cmd}},
["motd"]={MotdOnOff,{user,data,env}},
["showmotd"]={ShowMotdInPm,{user,env}},
["noclean"]={NoCleanUser,{user, data,env}},
["shownoclean"]={ShowNoClean,{user,env}},
["seen"]={SeenUser,{user, data,env}},
["cleanusers"]={docleanusers,{user,env}},
["postmsg"]={postmsg,{user,data,env}},
["readmsg"]={readmsg ,{user,data,env}},
["inbox"]={inbox,{user,env}},
["delmsg"]={delmsg,{user,data, env}},
["whois"]={doWhois,{user, data, env}},
["lagtest"]={lagtestOnOff,{user,data,env}},
["addwelcome"]={addwelcome,{user,data,env}},
["showwelcome"]={showwelcome,{user,data,env}},
["delwelcome"]={delwelcome,{user,data,env}},
["addleave"]={addleave,{user,data,env}},
["showleave"]={showleave,{user,data,env}},
["delleave"]={delleave,{user,data,env}},
["searchlog"] ={logfinder,{user,data,env}},
["cleanlogs"]={logcleaner,{user,env}},
["watchkicks"]={kicksignup,{user,data,env}},
["topic"]={changetopic,{user,data,env}}}
Definitely... the [n] field should be in EVERY table, but you shoudln't access it directly, but through the table methods (getn, setn and also insert and remove)... here are definitions from the LUA reference...
Most functions in the table library assume that the table represents an array or a list. For those
functions, an important concept is the size of the array. There are three ways to specify that size:
? the field "n" ? When the table has a field "n" with a numerical value, that value is assumed
as its size.
? setn ? You can call the table.setn function to explicitly set the size of a table.
? implicit size ? Otherwise, the size of the object is one less the first integer index with a nil
value.
Quoteif table==nil means the table does not exist
if table=={} means table exists but empty
Hope this works 4 u.
nop, doesnt work tried that already, and the table is "NEVER" nil,
QuoteOriginally posted by The_Fox
Definitely... the [n] field should be in EVERY table, but you shoudln't access it directly, but through the table methods (getn, setn and also insert and remove)... here are definitions from the LUA reference...
Most functions in the table library assume that the table represents an array or a list. For those
functions, an important concept is the size of the array. There are three ways to specify that size:
? the field "n" ? When the table has a field "n" with a numerical value, that value is assumed
as its size.
? setn ? You can call the table.setn function to explicitly set the size of a table.
? implicit size ? Otherwise, the size of the object is one less the first integer index with a nil
value.
so basically what u guys are saying, is that a table should "always" have and [n] field?,eg. if table["stuff"]== nil then...??
hmm, maybe thats why when i try to attach a string to it, it doesnt work, well that other method im sure it works but its more troublesome in terms of programming, u need to create a function, to enumerate each item or something similar!
Movie = {"Meet","The","Fockers"}
print(getn(Movie))
Cars = {["BMW"] = 1, ["Toyota"] = 2, ["Volvo"] = 3}
print(getn(Cars))
Planets = {[1] = "Mercurius", [2] = "Venus", [3] = "Earth"}
print(getn(Planets))
Cars.n = 3;
print(getn(Cars))
Output values :
3
0
3
3
So the answer to your question bast is no, you can however iterate over it and get the amount
I still insist.. you should use the isEmpty function I posted earlier in order to confirm that an assoc table has/or hasn't got values.
It is very easy and doesnt add load to mem or cpu if declared a local wherever you need it..
tab={}
if table.getn(tab)==0 then
print("Empty.")
end
results in:
---------- Lua Debugger ----------
Empty.
Output completed (0 sec consumed) - Normal Termination
QuoteOriginally posted by Herodes
I still insist.. you should use the isEmpty function I posted earlier in order to confirm that an assoc table has/or hasn't got values.
It is very easy and doesnt add load to mem or cpu if declared a local wherever you need it..
yap herodes, ur "insisting" payed off, it worked perfectly thanks, :D
QuoteOriginally posted by kepp
Movie = {"Meet","The","Fockers"}
print(getn(Movie))
Cars = {["BMW"] = 1, ["Toyota"] = 2, ["Volvo"] = 3}
print(getn(Cars))
Planets = {[1] = "Mercurius", [2] = "Venus", [3] = "Earth"}
print(getn(Planets))
Cars.n = 3;
print(getn(Cars))
Output values :
3
0
3
3
So the answer to your question bast is no, you can however iterate over it and get the amount
The second case didn't work because you're not caring about the number of items in the table, you just hope that LUA counts them correctly... and because table.getn doesn't count them, it just takes the "first integer with nil value", it's quite a risk. An honourable man (just playing with words, no worries :-) ) should write:
Cars = {["BMW"] = 1, ["Toyota"] = 2, ["Volvo"] = 3}
table.setn(Cars, 3)
print(getn(Cars))
QuoteOriginally posted by Herodes
I still insist.. you should use the isEmpty function I posted earlier in order to confirm that an assoc table has/or hasn't got values.
It is very easy and doesnt add load to mem or cpu if declared a local wherever you need it..
But anyway, I was just trying to explain some things to others, who posted here... the ultimate answer to the first question is what Herodes said... isEmpty is (as far as I know) the only valid way to learn if a table is empty or not :-)
QuoteOriginally posted by The_Fox
QuoteOriginally posted by Herodes
I still insist.. you should use the isEmpty function I posted earlier in order to confirm that an assoc table has/or hasn't got values.
It is very easy and doesnt add load to mem or cpu if declared a local wherever you need it..
But anyway, I was just trying to explain some things to others, who posted here... the ultimate answer to the first question is what Herodes said... isEmpty is (as far as I know) the only valid way to learn if a table is empty or not :-)
so in a brief of analysis of all the posts, herodes is right (and answered my question) the rest of u are wrong, excluding the_fox, :D.
QuoteOriginally posted by The_Fox
QuoteOriginally posted by kepp
Movie = {"Meet","The","Fockers"}
print(getn(Movie))
Cars = {["BMW"] = 1, ["Toyota"] = 2, ["Volvo"] = 3}
print(getn(Cars))
Planets = {[1] = "Mercurius", [2] = "Venus", [3] = "Earth"}
print(getn(Planets))
Cars.n = 3;
print(getn(Cars))
Output values :
3
0
3
3
So the answer to your question bast is no, you can however iterate over it and get the amount
The second case didn't work because you're not caring about the number of items in the table, you just hope that LUA counts them correctly... and because table.getn doesn't count them, it just takes the "first integer with nil value", it's quite a risk. An honourable man (just playing with words, no worries :-) ) should write:
Cars = {["BMW"] = 1, ["Toyota"] = 2, ["Volvo"] = 3}
table.setn(Cars, 3)
print(getn(Cars))
Yea, and it was exactly what i wanted to show
If you look behind the scenes at Movie table it would look just as planets, where there is an index before the key :D
found (http://article.gmane.org/gmane.comp.lang.lua.general/14772) a better-done isEmpty ;)
function isEmpty(t)
return (next(t) == nil);
end
QuoteOriginally posted by Herodes
found (http://article.gmane.org/gmane.comp.lang.lua.general/14772) a better-done isEmpty ;)
function isEmpty(t)
return (next(t) == nil);
end
nice, good research, well done !! only 3 lines of coding now !
Indeed, less coding and more simplicity.
Keep it up.