Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the hashtag/number sign in roblox Lua?

Asked by 7 years ago

so I was looking at some sources and found this:

for i = 2, #p do

does anyone know what the "#" means?

0
Maybe a thumbs up would be great support, or actually leave my answer accepted rather than accepting it and then unaccepting it? I didn't spend 14 minutes going to wiki pages to give you helpful links just so I can get unaccepted again. EzraNehemiah_TF2 3552 — 7y

4 answers

Log in to vote
6
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

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).

Ad
Log in to vote
3
Answered by 7 years ago

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!

0
thank you Awsomeman511 30 — 7y
0
It is important to note that the length operator will not count table elements with non-numerical keys. E.g: `local t = { ["key"] = 2 } print(#t)`, would output '0'. Link150 1355 — 7y
0
Also you should know that # can also count the characters in a string like bluetaslem said. EzraNehemiah_TF2 3552 — 7y
0
alright, thx Awsomeman511 30 — 7y
0
also, thx for adding some additional notes! :) starlebVerse 685 — 7y
Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

What does the "#" operator do?

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.


Here are examples:

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.

Operators

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!

0
thanks too! Awsomeman511 30 — 7y
Log in to vote
-2
Answered by 7 years ago

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.

0
thanks, im gonna wait a bit for more answers because that was kind of hard to understand lol Awsomeman511 30 — 7y

Answer this question