Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Pound/Hashtag/TTT symbol?

I am quite new to Lua, so I was scavenging through the example code and I found this:
local n = #array
What does the "#" do? I looked all around the reference.

Comments

  • IgnatzIgnatz Mod
    edited March 2016 Posts: 5,396

    The hash # gives you the number of numeric items in the table. As long as the items are all in number order, it should give a reliable result

    You can also use it to add items to a table, eg tbl[#tbl+1]="abc", which adds an item using a table index one greater than the current table count.

    It won't give you the right answer if you have gaps in your table (eg no tbl[3]) or if you have text keys in your table, eg tbl["abc"]=3. In these cases, you are better to loop through your table using the "pairs" approach.

  • Posts: 2,020
    It will also return the length of a string
  • Posts: 79

    Cool! Thank you all for the help.

Sign In or Register to comment.