generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Length of non-numeric table?

Previous Thread :: Next Thread 
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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 is not online. 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 is not online. 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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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 is not online. 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 is not online. 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
benny75890 is not online. benny75890
Joined: 15 Aug 2009
Total Posts: 23
18 Jul 2013 08:04 PM
dewd why arent they your role models now?
Report Abuse
dewd45 is not online. 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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
18 Jul 2013 08:25 PM
if next(t) then?
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
18 Jul 2013 08:42 PM
Yup that works too
Report Abuse
Christbru01 is not online. Christbru01
Joined: 03 Apr 2010
Total Posts: 649
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
ukwarrior6 is not online. ukwarrior6
Joined: 06 Sep 2009
Total Posts: 485
18 Jul 2013 09:32 PM
@ Christbru01

Surely that won't work, as members of his tables aren't numerical?
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
18 Jul 2013 09:32 PM
That wont work, as its for tables with non-numeric indexes.
Report Abuse
Christbru01 is not online. Christbru01
Joined: 03 Apr 2010
Total Posts: 649
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
Christbru01 is not online. Christbru01
Joined: 03 Apr 2010
Total Posts: 649
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
19 Jul 2013 06:35 AM
Thanks guys. Much appreciated.
Report Abuse
EivkizArysui is not online. EivkizArysui
Joined: 14 Jul 2013
Total Posts: 201
19 Jul 2013 06:56 AM
@christ: what if the element in the table isn't bound to index 1?
Report Abuse
ukwarrior6 is not online. ukwarrior6
Joined: 06 Sep 2009
Total Posts: 485
08 Nov 2013 05:52 PM
Then it's a poorly organised table.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image