182 posts
Posted 13 June 2013 - 03:02 AM
Hai,
I have once again started reviewing NeverCast's code again for 'becoming top level' and I am once again completely confused. I am attempting to rewrite is for a simple OS loader so I can include advance functions within my programs. I have looked at his code again and am wondering why he has written the following like so:
table.size = function(tbl)
local count = 0
for _,__ in pairs(tbl) do
count = count + 1
end
return count
end
local function getParentShell()
for i = 0, 5 do
local env = getfenv(i)
if table.size(env) == 1 then
local i,v = next(env) -- Grab first
if i == "shell" then
return v
end
end
end
return nil
end
My main question about it is why has he made a function to get a table size. What has stopped him from doing #env and using this instead table.size(env)?
56 posts
Location
Victoria, AUS
Posted 13 June 2013 - 04:08 AM
Using # to find the length of a table only returns the length of the entries with a number as the index. Since the environment table uses strings as the indexes, he has to use his own function.
7508 posts
Location
Australia
Posted 13 June 2013 - 04:47 AM
As chiloxsan stated the # only gets the count of the indexed values. To expand on that the table.getn and table.maxn both also count the indexed values. Example:
local t = { this = 1 }
print( #t )
will output 0. This is why Nevercast made his own function, to count both indexed and key/value pairs…
7083 posts
Location
Tasmania (AU)
Posted 13 June 2013 - 07:52 AM
Another catch to #env is that it stops counting when it hits the first null value. For example, say you defined:
env = {7,nil,5}
#env would return 1.
table.getn(env) would return 3. I haven't heard of
table.size() before (should read that API some day), but what I'm reading of it now suggests it'd return 2 here (though I'd recommend testing it to be sure).
7508 posts
Location
Australia
Posted 13 June 2013 - 07:54 AM
I haven't heard of table.size() before (should read that API some day)
NeverCast defined it himself… it doesn't actually exist…
table.size = function(tbl)
7083 posts
Location
Tasmania (AU)
Posted 13 June 2013 - 08:34 AM
Ah. Well done me for missing the first line - I think it's my bed time.
7508 posts
Location
Australia
Posted 13 June 2013 - 08:36 AM
Ah. Well done me for missing the first line - I think it's my bed time.
Its ok, well all have momentary lapses in judgement and thinking, I think it was Orwell that had one today too, maybe it was yesterday. Bedtime… at 22:36 how early?! :P/>
7083 posts
Location
Tasmania (AU)
Posted 13 June 2013 - 08:46 AM
Usually I'd indeed call it early, but I'm transitioning from a late night shift today to a morning shift tomorrow, with about six hours sleep last night (er, this morning) starting from 2am…
I'm too young to feel old!
Anyway, yeah, point is Lua doesn't have a function to track how many indexes are actually in a table. It has commands that may give you that answer if you populate the table a certain way, but they're not accurate in all instances (eg the sample table I provided above).
I'd let myself think I missed a function, but you really do have to define one yourself if you want that kind of accuracy.