Wouldn't "invert" mean that you actually swap keys and values, instead of setting all values to "true"? That way you could not only check, if an entry exists, but also locate it in the original table.Wojak wrote:Inverting an array –
What techniques that everyone should know?
Re: What techniques that everyone should know?
Check out my blog on gamedev
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: What techniques that everyone should know?
Check if a table is empty:
Check if a table is an array:
Code: Select all
local is_empty = function(t)
return next(t) == nil
end
Code: Select all
local function is_array(t)
local maximum, count = 0, 0
for k, _ in pairs(t) do
if type(k) ~= 'number' or k < 1 or math.floor(k) ~= k then return false end
maximum = math.max(maximum, k)
count = count + 1
end
return count == maximum
end
When I write def I mean function.
Re: What techniques that everyone should know?
Note: Doesn't catch arrays in the format - t={ {x=1,y=2},...} as used by the Hardon collider.kikito wrote:
Check if a table is an array:
Code: Select all
local function is_array(t) local maximum, count = 0, 0 for k, _ in pairs(t) do if type(k) ~= 'number' or k < 1 or math.floor(k) ~= k then return false end maximum = math.max(maximum, k) count = count + 1 end return count == maximum end
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: What techniques that everyone should know?
I don't know what you mean.Ref wrote:Note: Doesn't catch arrays in the format - t={ {x=1,y=2},...} as used by the Hardon collider.
Code: Select all
> t = {{x=1,y=2}, {x=2,y=3}}
> print(is_array(t))
true
When I write def I mean function.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: What techniques that everyone should know?
Yay, I like this. I have something similar in Moses.kikito wrote:Check if a table is an array
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: What techniques that everyone should know?
Your implementation is very elegant. It's equivalent to this:Roland_Yonaba wrote:Yay, I like this. I have something similar in Moses.kikito wrote:Check if a table is an array
Code: Select all
local function isArray(t)
local count = 0
for _,_ in pairs(t) do count = count + 1 end
return count == #t
end
EDIT: I remembered.
Code: Select all
local a = {1,nil,3,a=4}
print(isArray(a)) -- true (depending on the Lua implementation)
When I write def I mean function.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: What techniques that everyone should know?
You are so true. The length operator can return falsy answers when given tables containing nil in some cases.kikito wrote:Code: Select all
local a = {1,nil,3,a=4} print(isArray(a)) -- true (depending on the Lua implementation)
Actually, your version of is_array handles it pretty well. While looping through the given table, in case we find a key which is non numeric (type(k)~='number') or which is lower than 1 (since Lua's array option base is set to 1), or which is not an integer (math.floor(k)~=k), we can safely assume that the given table is not a standard Lua array. This trivially solves the problem, as there is no need to rely on the length operator. Yay.
May I reuse it in Moses ?
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: What techniques that everyone should know?
Sure, be my guest.Roland_Yonaba wrote:May I reuse it in Moses ?
EDIT:
For the record, I think that if you check the numericality of the keys first, then you can compare count with #t and return that as a result.
Code: Select all
local function is_array(t)
local count = 0
for k, _ in pairs(t) do
if type(k) ~= 'number' or k < 1 or math.floor(k) ~= k then return false end
count = count + 1
end
return count == #t
end
When I write def I mean function.
Re: What techniques that everyone should know?
My bad. My testing invalid.kikito wrote:I don't know what you mean.Ref wrote:Note: Doesn't catch arrays in the format - t={ {x=1,y=2},...} as used by the Hardon collider.
Code: Select all
> t = {{x=1,y=2}, {x=2,y=3}} > print(is_array(t)) true
Thanks for the function.
Re: What techniques that everyone should know?
This wouldn't work with embedded nils. Though technically it's not an array anymore when it has an embedded nil, so maybe you're semantically correct.kikito wrote:For the record, I think that if you check the numericality of the keys first, then you can compare count with #t and return that as a result.
But since the # operator has some weird edge cases, and it could change in the future, I opted for a less implementation-dependant solution.Code: Select all
local function is_array(t) local count = 0 for k, _ in pairs(t) do if type(k) ~= 'number' or k < 1 or math.floor(k) ~= k then return false end count = count + 1 end return count == #t end
To demonstrate, this is what Lua does with an embedded nil:
Code: Select all
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
> =#t
20
> t[5] = nil
> =#t
20
> print(t[5])
nil
Who is online
Users browsing this forum: No registered users and 2 guests