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

Why is #table always returning 0?

Asked by 9 years ago

I'm writing a script which rely's a ton on the number of objects in a table to properly tween them, but when a function is called which checks #mytable, the result it gets is always 0, and despite the fact that the table does have data in it (the script adds a object to the table before it runs said function, so the table must be full), it always returns 0, so if any one knows what the issue is please tell me. Here is a script which may help diagnose the issue:

01local mytable = {}
02 
03function getIncerement()
04    print(#mytable) --always outputs 0
05    if #mytable > 1 then
06        return 0.5
07    else
08        return 0
09    end
10end
11 
12function func(name)
13    local obj --I would put my object here
14    mytable[name] = obj
15    local i = getIncerement()
View all 21 lines...

Aaaaaaaaaand ya my issue is when the getIncerement() function is called always #mytable returns 0, if you can figure out the issue great, because this is threatening the cleanliness of a script of mine.

Thanks ~chabad360

2
You can not name a table table, or else the game will assume you're referring to the "table" functions. M39a9am3R 3210 — 9y
0
it's also important to remember that the #x syntax won't work as might be expected when dealing with dictionaries User#11893 186 — 9y
0
I know this is just for the question, but it's not like that in the actual script chabad360 34 — 9y
2
IT CERATINLY WOULD BE GREAT IF WE GOT THE SCRIPT LegitimatlyMe 519 — 9y
View all comments (3 more)
0
Well we can't exactly help you without any script.... NinjoOnline 1146 — 9y
0
I'll put a script which has good resemblance to the real one, but I don't know if it will help... chabad360 34 — 9y
0
Put the actual one, this still doesn't make complete sense! General_Scripter 425 — 9y

2 answers

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
9 years ago

Consult Section 2.5.5 of the Lua 5.1 Reference Manual:

"The length of a table t is defined to be any integer index n such thatt[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero."

The result of the length operator (#), when applied to a table, will be correct if each integer index from 0 to n (for some value of n) is non-nil.

I propose two possible solutions:

  1. Introduce a new variable (e.g. myTableLength). Update it manually whenever objects are added or removed from the table. In your func, you would write myTableLength = myTableLength + 1.

  2. Use a generic table entry counting function to compute the length:

1function tableLength(t)
2    local count = 0
3    for _ in pairs(t) do
4        count = count + 1 end
5    return count
6end
0
I thought of that but I really don't want to but I guess I have to... chabad360 34 — 9y
Ad
Log in to vote
3
Answered by 9 years ago

This is because your table is composed in dictionary format. There are two kinds of tables:

Arrays (Lists)

A table comprised of only numeric keys (i.e, just having single elements in a table)

Dictionaries (Maps)

A table comprised of manually defined keys (i.e, assigning variable-like conditions to a value inside the table)

Dictionary (or Map) example

1-- "x","y","z" overwrites our numeric keys 1,2,3.
2 
3local Dictionary = {
4    x = "hello",
5    y = "i am",
6    z = "a dictionary"
7}

Array (or List) example

1-- Nothing overwrites our default numeric keys, therefore they remain 1,2,3, allowing us to use the # operator on the table to return the number of elements.
2 
3local Array = {
4    "hello",
5    "i am",
6    "an array"
7}

I would go on, but honestly everything I'd explain here I've already explained in another answer that you may find very useful here:

https://scriptinghelpers.org/questions/25964/what-are-tables#29555

Answer this question