so I was looking at some sources and found this:
for i = 2, #p do
does anyone know what the "#" means?
It's an operator that means "length of".
For a string, that's the number of characters (letters, numbers, spaces, punctuation marks) in it:
print(#"cat") --> 3 print(#"hello world") --> 11
For a list, that's the number of things in it:
print(#{"a", "b", "c"}) --> 3 print(#{ {1,2}, {3,4}, {5,6} }) --> 3 (three tables -- the things inside those tables don't count)
This is really useful information to know for a list.
list[1]
is the first thing in a list. list[2]
the second. list[3]
the third.
The last thing in the list is list[#list]
. E.g. in a list of ten things, the last thing is list[10]
. If the list has ten things, then #list
is the same as 10
.
Doing for i = 2, #p
probably is being used to "do this for each of the things in the list p
starting from the second thing"
#
is a "prefix operator" just like the -
is in print(-x)
.
It means the length of a table. Let me show you a few examples.
local t = {1, 5, 7, 21} print(#t)
This will print 4 because there are 4 values in the table: 1, 5, 7 and 21.
local t = {1, 5, 6, 7, 8, "Smile", true} -- A table can store other things too! for i = 1, #t do --Literates from 1 to 7 (the length of the table "t"). print(t[i]) end
This will print all the things in the table.
#
is useful when you are trying to do something if there are enough values in a table.
Such as:
local t = {1, 3, 5, 7, 9, 11} if #t > 3 then print("There are more than 3 numbers in the table.") end
This will print, "There are more than 3 numbers in the table." because the length of the table is more than 3.
That is all I can explain. If you have any problems, please leave a comment below. Thank you and I hope this will help you!
The #
operator returns the amount of items inside of a table. A Table is a variable that can hold multiple values: http://wiki.roblox.com/index.php?title=Table.
You get specific values by locating them by their key.
--Tables can hold multiple values local table = {1,3,true,false,nil,workspace.Part,function() print(1) end} print(table[2]) --Prints "3" which is the 2nd value inside the table.
Tables can be split into different categories of tables. Arrays
and Dictionaries
. Arrays have their values labeled numerically. So their key is a number:
local table = {workspace.Part,true} print(table[2]) --table[2] only takes the 2nd value, which is true. It prints true.
Dictionaries are arrays but their keys can be any value. From Integers
to Objects
and even Strings
.
local table = {["String"] = "Text", [workspace.Part] = "A Block.", [4] = true} print(table["String"]) --Prints out Text.
They're called dictionaries because the keys are being defined. Note that a key cannot be named nil.
These are things you put in between or around values to preform an operation on it. The specific one we'll be talking about is the #
operator.
local table = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} --Wow that's a lot of 1's! How many are there?! print(#table) --This will perform an operation that'll count all the values in a table.
We can use these in loops like you said:
local p = {1,2,3,4,5,6,7,8,9,10} for i = 1,#p do end --Does the loop 10 times because there are 10 values in p.
Also, most operators come with it's own associated metamethod
, each metamethod can effect a metatable
. For example, #
's associated operator is _le
).
Hope it helps!
There is no easy way to define this. But think of it as "number of". So lets say p is referring to a Table, then "i" equals 2, in "number of p". Sorry if this is vague but I hope it helped a bit.