Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 06 Jan 2015 07:25 PM |
tab = {tab2 = {}, tab3 = {}} print(#tab) --> 0
tab = {{}, {}} print(#tab) --> 2
Why? |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 06 Jan 2015 07:27 PM |
Because one is a dictionary table and the other isn't
local function getKeys(tab) local output = {} for key, _ in next, tab do table.insert(output, key) end return output end
tab = {tab2 = {}, tab3 = {}}
print(#getKeys(tab)) |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2015 07:28 PM |
| The # operator only return the number of INTEGER KEYS |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 06 Jan 2015 07:28 PM |
for key, _ in next, tab do end
What is the next keyword there, I have never seen that before? |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 06 Jan 2015 07:30 PM |
Have you seen pairs before?
They are the same thing
Because pairs is actually doing next |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 06 Jan 2015 07:31 PM |
Yes I normally would use,
for i,v in pairs(tab) do end |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 06 Jan 2015 07:32 PM |
Its the same thing, because this is what pairs is doing
local function pairs(tab) return next, tab end
local a = { a = 1; b = 2; c = 3; }
for key, value in pairs(a) do print(key, value) end |
|
|
| Report Abuse |
|
|
|
| 06 Jan 2015 07:35 PM |
| They aren't the same thing, since pairs uses next. Not to mention next also allows an index to be given to determine the start position. |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 06 Jan 2015 07:36 PM |
We've been through this before
To me they are the same because they do the same thing
For the same reason
local a = 1 local b = a
Now a and b are the same even though b is using a |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 06 Jan 2015 07:37 PM |
| Thanks for the information I never knew that! |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Jan 2015 07:38 PM |
Pairs is a different function than next in memory, the b = a does not apply since it just copies the value instead of stores as a reference.
And 'The # operator only return the number of INTEGER KEYS' Is not true. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Jan 2015 07:39 PM |
| Although yes, pairs does call next but it's not the same thing :) |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 06 Jan 2015 07:39 PM |
| I didn't say they are the same in memory I said they are the same because pairs uses next |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Jan 2015 07:41 PM |
| Same results would technically be more accurate :) |
|
|
| Report Abuse |
|
|
iiEssence
|
  |
| Joined: 18 Jun 2014 |
| Total Posts: 3467 |
|
|
| 06 Jan 2015 07:42 PM |
cnt ur a beginner scripter remember
shhh |
|
|
| Report Abuse |
|
|