|
| 18 Jul 2013 07:51 PM |
How would I tell the size of a table that isn't using numeric indices? For example:
local t = { A = 32; B = 64; C = 128; }
print(#t) > 0
Since '#' counts all numeric indexes.
Is there a way to tell the size of that table __WITHOUT__ iterating through it? |
|
|
| Report Abuse |
|
|
Ozzypig
|
  |
| Joined: 27 Mar 2008 |
| Total Posts: 4906 |
|
|
| 18 Jul 2013 07:51 PM |
Nope. Not a single way.
~Ozzypig
PS someone prove me wrong |
|
|
| Report Abuse |
|
|
badcc
|
  |
| Joined: 18 Jan 2009 |
| Total Posts: 3170 |
|
|
| 18 Jul 2013 07:54 PM |
| You can't, sadly. You'll have to loop through it. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 07:56 PM |
GAWD stupid Lua. They better add that in like Lua 5.3 or something.
All I want to know is "is this table empty or not?" But nope, I gotta do this now:
function IsEmpty(t) for _,v in pairs(t) do return true end return false end |
|
|
| Report Abuse |
|
|
dewd45
|
  |
| Joined: 08 Mar 2009 |
| Total Posts: 11180 |
|
|
| 18 Jul 2013 08:00 PM |
ozzy and crazy? wow, you two used to be my role models
#swagyolo |
|
|
| Report Abuse |
|
|
Ozzypig
|
  |
| Joined: 27 Mar 2008 |
| Total Posts: 4906 |
|
|
| 18 Jul 2013 08:00 PM |
You could hack something together with the pairs() command.
if pairs(t)() then print("Has something in it") end
Because pairs() returns an iterator function.
~Ozzypig |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 08:04 PM |
| dewd why arent they your role models now? |
|
|
| Report Abuse |
|
|
dewd45
|
  |
| Joined: 08 Mar 2009 |
| Total Posts: 11180 |
|
|
| 18 Jul 2013 08:05 PM |
because I've already learned im not going to be that good of a scripter.
#swagyolo |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 08:19 PM |
Thanks ozzy! Errored like that though, but just had to throw the table into the second call as well. Works nicely
if pairs(t)(t) then ... |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Jul 2013 08:28 PM |
| Because pairs just returns an iterator function( hence two (t)'s), so why not cut straight to an iterator function and only use a single (t)? |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Jul 2013 09:30 PM |
or you could just do if t[1] then... mainly cuz this calls for the first item in a table, and if there are no items in the table then it'd look for a metatable setting, and if there are none then it'd return nil, and then the if then would treat nil as false, and would not run... x3
Note: This is a much easier way for regular tables, though it may cause problems if you use metatables and such... so for most scripters it's no problem to use that |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 09:32 PM |
@ Christbru01
Surely that won't work, as members of his tables aren't numerical? |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 09:32 PM |
| That wont work, as its for tables with non-numeric indexes. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 09:37 PM |
Oh yeah, Sorry for that, I kinda derped a bit cx Anyways... only other thing I could think of at this moment is to check it with an empty table, like "if t == {} then" |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 09:39 PM |
| That compares the memory addresses, so will always be false. Anyway, the answer is to do if next(t) then EXISTS or DOESNT_EXIST end |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2013 10:02 PM |
| Oh yeah... I forgot about next... and now that you've jogged my memory, you could also use rawget(t,1) and test if it's nil... but yeah, I think for what you're looking for next(t) would be best, props to Notunknown99! |
|
|
| Report Abuse |
|
|
|
| 19 Jul 2013 06:35 AM |
| Thanks guys. Much appreciated. |
|
|
| Report Abuse |
|
|
|
| 19 Jul 2013 06:56 AM |
| @christ: what if the element in the table isn't bound to index 1? |
|
|
| Report Abuse |
|
|
|
| 08 Nov 2013 05:52 PM |
| Then it's a poorly organised table. |
|
|
| Report Abuse |
|
|