Some small table search script that I wrote several month ago,
Based on my answer on
[Link]
Code: Select all
function inArray(array,input,index)
if (type(array) ~= 'table') then
return false;
end
for key,value in pairs(array) do
if (type(value)=='table') then
local status = inArray(value,input,index);
if (status) then
return status
end
-- elseif ((tonumber(input) or input) == (tonumber(value) or value) and (not index and key == index or true)) then
elseif (((input == value) or (tonumber(input) == tonumber(value))) and (not index and key == index or true)) then
return true;
end
end
return false
end
function table.search(input,value,case,index_check,_return,returnall,exclude) -- some utility i wrote for someone long time ago
-- print("Start search",tostring(input))
-- processMessages();
-- sleep(100);
local cache,info,foundresults,func = {},{},false
local tostring,type = tostring,type
local output = function(input,value,case,index_check,_return, returnall,exclude)
if (type(input)~='table') then
return nil;
else
local ts_input = tostring(input);
local cur_cache = cache[ts_input];
if (cur_cache) then
return false;
end
if (exclude and type(exclude) == 'table' and inArray(exclude,input)) then
return false;
end
cache[ts_input] = ts_input
if (type(value) == 'table' and value == input) then
return true;
end
for key,object in pairs(input) do
if (index_check) then
if (case == "find" and type(value)=='string' and type(key)=='string') then
if (key:lower():find(value:lower())) then -- to avoid exit the loop
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
end
elseif (case and type(value)=='string' and type(key)=='string') then
if (value:lower() == key:lower()) then -- to avoid exit the loop
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
end
else
if (key == value) then
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
elseif(type(object)=='table' and not cache[tostring(object)]) then
local result = func(object,value,case,index_check,_return, returnall,exclude)
if (result and not returnall) then
return result;
end
end
end
else
if (case == "find" and type(value)=='string' and type(object) == 'string') then
if (object:lower():find(value:lower())) then
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
end
elseif (case and type(value)=='string' and type(object) == 'string') then
if (value:lower() == object:lower()) then
-- cache = {};
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
end
elseif(type(object)=='table' and not cache[tostring(object)]) then
if (value == object) then
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
else
local result = func(object,value,case,index_check,_return, returnall,exclude)
if (result and not returnall) then
return result;
end
end
else
if (object == value) then
if (returnall) then
foundresults = true;
info[ts_input] = input
else
return (_return and input or true);
end
end
end
end
end
end
if (foundresults) then
return info,true;
else
return false;
end
end
func = function (...) return output(...) end;
local op = output(input,value,case,index_check,_return, returnall,exclude);
cache = {};
return op
end
Usage is simple, but this function makes lives easier and fairly fast (unless you run an array with thousands keys and has more than 3 dimensions, it should work very fast).
usage:
Code: Select all
--[[-- table.search(
@param1 table(table),
@param2 value(value to search),
@param3 case-sensitive(true/false),
@param4 type (index or value type)
@param5 return relative table
@param6 return relative table, continues the search and returns all data
@param7 exclude table (to be checked using a simple inArray function)
)
-- checks if these two indexes were set any where in the launchArgs table and checks their type
if (table.search(t,"hi",true)) then-- table,name,case(true to ignore case)
-- do your stuff
end
-- ]]
So you may use this function to search based key inside your table, and search based on value, and since this function returns the table in which you've found in your searches, if both results equal it's likely to be the very same table, but just in case it isn't it, recommended defining uniquely your table by any key or any value so results would be more accurate.
In short words
Code: Select all
table.search(check key) == table.search(check value entry) or table.search(unique key)
table.search result is the parent table (or the supplied table) of the property or key value where input was found.
So doing:
Code: Select all
bla = table.search( ...)
for k in ipairs(bla) do -- use ipairs when you index by numbers, otherwise use pairs.
bla[k] = nil -- nils everything on that table, to delete it you must remove any reference of it,
end -- calling garagecollector will force c.e to free it immediately assuming there is no refernce to it.
Some quick method to create a dir (Silently).
Code: Select all
YOURPATH = "%APPDATA%\\blah"
shellExecute('cmd.exe', '/c mkdir "' .. YOURPATH .. '"', nil, false);
let me know if you got any questions.