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 » Scripters
Home Search
 

Re: #Table printing 0?

Previous Thread :: Next Thread 
Codys4x4 is not online. Codys4x4
Joined: 14 Oct 2006
Total Posts: 2312
27 Aug 2015 01:48 AM
Why does this print 0?

Stock = {
["9910070"] = math.random(1,3),
["88885069"] = math.random(1,3),
["26943368"] = math.random(1,3),
["1031429"] = math.random(1,3),
["1323384"] = math.random(1,3)
}

print("Stock: "..#Stock)

while #Stock <= 1 or print("Restart") do
wait(1)
print(#Stock)
end
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 01:48 AM
That is a dictionary because all of your inserts are named with strings and dictionaries always return 0 to the # operator.
Report Abuse
Codys4x4 is not online. Codys4x4
Joined: 14 Oct 2006
Total Posts: 2312
27 Aug 2015 01:51 AM
Is there anyway to count them like that, or will I have to add each one then print the total?
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 01:54 AM
You could count like this:

local NumStockItems = 0
for _, v in pairs(Stock) do NumStockItems = NumStockItems + 1 end


but if your keys are numbers anyways then just do something like this. It won't make it any slower:


Stock = {}
Stock[9910070] = math.random(1,3),
Stock[88885069] = math.random(1,3),
Stock[26943368] = math.random(1,3),
Stock[1031429] = math.random(1,3),
Stock[1323384] = math.random(1,3)
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 01:55 AM
If you do the bottom one you can still use #
Report Abuse
ash877 is not online. ash877
Joined: 18 Feb 2008
Total Posts: 5142
27 Aug 2015 02:13 AM
no you can't
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 02:15 AM
Yah huh
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 02:17 AM
Whoops he is right use the top one
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Aug 2015 02:18 AM
No, you can't. And you there is not distinction between dictionaries//lists//arrays in lua.

It's just tables.

The len operator starts at 1, and looks for sequential entries, stopping when
table[currentEntry+1] == nil

This means the len operator will never count zero, and doesn't count no sequential entries.
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 02:19 AM
You could've just waited for me to figure it out... I figured it out before you posted that
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 02:19 AM
The #operator still doesn't work with dictionaries it only works with ordered tables!
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
27 Aug 2015 02:20 AM
Stock = {
["9910070"] = math.random(1,3),
["88885069"] = math.random(1,3),
["26943368"] = math.random(1,3),
["1031429"] = math.random(1,3),
["1323384"] = math.random(1,3)
}
setmetatable(Stock,{__len = function(self) local N = 0 for i,v in next, self do N = N + 1 end return N end})

-- Now # works :)









Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Aug 2015 02:21 AM
Except it doesnt because tables do not recognize the len operator from metatables. Userdata does.
Report Abuse
gooey333 is not online. gooey333
Joined: 24 Mar 2013
Total Posts: 1208
27 Aug 2015 02:22 AM
@chim


That is a very complicated method. Just use for
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
27 Aug 2015 02:28 AM
"Except it doesnt because tables do not recognize the len operator from metatables. Userdata does."

You obviously have little knowledge of metatables.
The len metamethod works 100% fine with tables.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Aug 2015 02:29 AM
In 5.2 yes, 5.1 no.
Run it in studio.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Aug 2015 02:30 AM
@chim Also see:
http://wiki.roblox.com/index.php?title=Metatable#Metamethods

Read the desc for __len
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
27 Aug 2015 02:51 AM
My PC is broken so I cannot use studio.

I will assume you and the wiki are wrong because:
1. None of the Lua 5.1 books I have read(Including PiL) have said the len metamethod did not respect tables.
2. I never used metatables in roblox, but outside roblox in almost all versions of Lua 5.0+ including 5.1 the len metamethod did respect tables.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Aug 2015 04:25 AM
@chim
http://www.lua.org/manual/5.1/manual.html#2.8
See section 2.8, "len"
Internally lua (_VERSION 5.1) respects the __len metamethod if data type it's used on is not a string and not a table.

For those types the primitive length is ALWAYS returned.

Section 2.5.5 defines primitive length in accordance to what I have previously said, although it's articulated better
http://www.lua.org/manual/5.1/manual.html#2.5.5


I don't know what you've been reading, but it's rather hard to refute their own docs (a quick google search will show this is a well documented issue and a prevalent source of confusion).
The primitive len operator works in 5.0+, but the len metamethod is not respected in versions prior to 5.2.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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