|
local dictionary = {
["Hello"] = "world",
["Swag"] = "too much"
}
print(#dictionary)
make it print 2 |
|
|
Create your own length function. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
local count = 0;
for key in next, dictionary do
count = count + 1;
end |
|
eLunateJoin Date: 2014-07-29 Post Count: 13268 |
function getn(t)
local r = 0;
for _ in next,t do
r = r+1;
end;
return r
end
Ye |
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
But you got to make it recurse :(
ADD RECURSE FUNCTION
I script -~ chimmihc |
|
|
|
@chim
wat? y recurse? this is fine. |
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
y no dis
{fish = {noob = {hi = {a = {etc = {etc = {}}}}}}}
WOT
I script -~ chimmihc |
|
|
becos i dont care about stupid sub dicitonaries or w/e they're called |
|
|
Why would you recursively find the number of elements in a table? I can't think of a reason for that. |
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
Cuz must find amount of total indices.
I script -~ chimmihc |
|
|
local dictionary = {
["Hello"] = "world",
["Swag"] = "too much"
}
local k = next(dictionary);
local n = 0;
while k do
n = n + 1;
k = next(dictionary, k);
end |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
what a punk using next, pairs is next but has a better name hahahahha
local mytbl = {a = 5; b = 10;};
local iter = pairs({});
local x, t = nil, 0;
while true do
local val = iter(mytbl, x);
if val ~= nil then x = val; else break; end
t = t + 1;
end
print(t); |
|
|
cnt is love, cnt is life
ok |
|
|
Does that work, KOT? I thought the second argument of next needed to be an integer, not a value...
lol my script is shorter than urs
local k = pairs(dictionary)
local n = 0
while k() do
n = n + 1
end |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
that wouldn't work hahahahahahhahaha and also if next only took integers how do you suppose it keeps track of non-numerical indices :(
|
|
|
Really? I thought it only worked on integers.
look:
local A = 0
local function Test(B, C)
print(B, C)
A = A + 1
if A <= 10 then
return 2
end
end
for _ in Test, {} do end
It always appends an integer value to the function you give it such as
"in next, Table do" |
|
|
And why wouldn't my script work? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
For one, pairs is a stateless iterator so it requires the last key (like next).
For two, pairs returns next, which in turn requires a table. |
|
eLunateJoin Date: 2014-07-29 Post Count: 13268 |
for nextVar, ..., somethingIdkWhatThisDoes in Func, arg2invariable, prevVar do
And then prevVar becomes nextVar and stuff ans Func is called with arg2invariable and prevVar |
|
|
Never mind, you're right. I guess I wasn't testing it right the other day. Strange. |
|
|
Oh, so that's what stateless means. o_o |
|
|
Iterators are so not my thing. |
|
|
Then again, that might be a result of nobody answering my questions related to them. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
[ Content Deleted ] |
|