Alright, now it's time for me to release my table functions!
Here they are (explanation below):
Explanation of usage of each function
table.find
This will search for values in tables (like isInArray but more advanced)
All of these, except for the last one will return true.
table.asort
This will sort tables in a more advanced way.
Will output:
As you see, it sorts the characters first (in this case #), then the numbers and at last alphabetical letters. In this case, it sorts by value, but if you wish you can make it sort by key!
table.clean
This will remove empty tables and/or strings inside a table.
Will output:
The empty tables were removed, but if you would have used CLEAN_EMPTY_STRING as second parameter, it would remove empty strings instead! You may use both strings and tables too
table.maxv
This will tell how many values a table contain (no matter what type of index).
The following code will output:
Why 5? Because, it does only count the current table, it does not deep search while counting. If you want to deep search, use countv (the function below)!
table.countv
This will tell how many values a table has in each "indent"...
Using the same table as in the example above (maxv), this will output:
Compare the results with the table, and you might understand how it works
table.totext
This will parse a table into text.
Will print:
table.tofile
This will save tables as they are in a file.
Will save the content of table "t" in table_file.lua as:
Enjoy,
Colandus
Here they are (explanation below):
PHP:
-- ~##TABLE::FUNCTIONS::BY::COLANDUS##~ --
FIND_NOCASE = 0
FIND_PATTERN = 1
FIND_PATTERN_NOCASE = 2
function table.find(t, v, c)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'find' (table expected, got "..type(t)..")", 2)
end
if v then
v = (c==0 or c==2) and v:lower() or v
for k, val in pairs(t) do
val = (c==0 or c==2) and val:lower() or val
if (c==1 or c==2) and val:find(v) or v == val then
return k
end
end
end
return false
end
SORT_VALUE = 0
SORT_KEY = 1
function table.asort(t, s)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'asort' (table expected, got "..type(t)..")", 2)
elseif type(s) ~= "number" then
return FALSE, error("bad argument #2 to 'asort' (number expected, got "..type(t)..")", 2)
end
local sorted = {}
for k, v in pairs(t) do
table.insert(sorted, {k, v})
end
local key = s == 1 and 1 or 2
table.sort(sorted, function(A,B) return string.lower(A[key]) < string.lower(B[key]) end)
return sorted
end
CLEAN_EMPTY_TABLE = 0
CLEAN_EMPTY_STRING = 1
function table.clean(t, ...)
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'clean' (table expected, got "..type(t)..")", 2)
end
if #arg == 0 then
table.insert(arg, 0)
end
for k,v in pairs(t) do
if type(v) == "table" then
if table.maxv(v) == 0 then
if table.find(arg, 0) then
t[k] = nil
end
else
t[k] = table.clean(v, unpack(arg))
end
elseif table.find(arg, 1) and type(v) == "string" and string.len(v) == 0 then
t[k] = nil
if table.maxv(t) == 0 then
return "kurwa"
end
end
end
return t
end
function table.maxv(t)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'maxv' (table expected, got "..type(t)..")", 2)
end
local i=0
for _ in pairs(t) do i=i+1 end
return i
end
do
local tcount = {total=0}
function table.countv(t, i)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'countv' (table expected, got "..type(t)..")", 2)
end
i=i or 1
tcount[i] = (tcount[i] or 0)+table.maxv(t)
tcount.total = tcount.total+table.maxv(t)
for _,v in pairs(t) do
if type(v) == "table" then
table.countv(v, i+1)
end
end
return tcount
end
end
function table.totext(t, i)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'totext' (table expected, got "..type(t)..")", 2)
end
i=i or 1
local indent=string.rep("\t", i)
local maxv, str, l = table.maxv(t), "", 0
for k,v in pairs(t) do
l=l+1
if type(k) == "string" then k = '"'..k..'"' end
str=str..indent.."["..k.."] = "
if type(v) == "table" then
str=str.."{\n"..table.totext(v, i+1).."\n"..indent.."}"
elseif type(v) == "string" or type(v) == "number" then
if type(v) == "string" then v = '"'..v..'"' end
str=str..v
else
str=str.."_"..type(v):upper().."_"
end
str=str..(l<maxv and ",\n" or "")
end
return str
end
function table.tofile(t, nf, tn)
-- function 100% by Colandus!
if type(t) ~= "table" then
return FALSE, error("bad argument #1 to 'tofile' (table expected, got "..type(t)..")", 2)
end
local f=io.open((nf or "table")..".lua", "w")
f:write((tn or "t").." = {"..table.totext(t).."\n}")
f:close()
return TRUE
end
Explanation of usage of each function
table.find
This will search for values in tables (like isInArray but more advanced)
PHP:
a = {"Colandus", "Colandus Rox", "HeLLo"}
print(table.find(a, "Colandus"))
print(table.find(a, "hello", FIND_NOCASE))
print(table.find(a, "Colandus Rox"))
print(table.find(a, "Col[a-z]+us", FIND_PATTERN))
print(table.find(a, "COL[a-z]+US", FIND_PATTERN_NOCASE))
print(table.find(a, "Hello World", FIND_NOCASE))
table.asort
This will sort tables in a more advanced way.
PHP:
x = {2, 9, 3, "#: First", 8, 2, 5, "Z: Last"}
for k,v in pairs(table.asort(x, SORT_VALUE)) do
print(k, v[1], v[2])
end
Code:
1 4 #: First
2 6 2
3 1 2
4 3 3
5 7 5
6 5 8
7 2 9
8 8 Z: Last
end
table.clean
This will remove empty tables and/or strings inside a table.
PHP:
t = {
"value",
["key"] = "value",
["empty"] = { },
{
1337,
{},
{
function() end
}
}
}
tb = table.clean(t)
print(table.totext(tb)
Code:
[1] = "value",
[2] = {
[1] = 1337,
[3] = {
[1] = _FUNCTION_
}
},
["key"] = "value"
table.maxv
This will tell how many values a table contain (no matter what type of index).
PHP:
t = {
1,
4,
5,
{
"huhu",
{
"asdds",
"heuaheua",
34234,
{
234,
["lol"] = 345,
654
}
},
12345,
"LoL"
},
"asd"
}
print(table.maxv(t))
Code:
5
table.countv
This will tell how many values a table has in each "indent"...
PHP:
for k,v in pairs(table.countv(t)) do
print(k,v)
end
Code:
1 5
2 4
3 4
4 3
total 16
table.totext
This will parse a table into text.
PHP:
t = {pos="lol", {pos={x=2,y=2,z=9}}}
print(table.totext(t))
Code:
[1] = {
["pos"] = {
["y"] = 2,
["x"] = 2,
["z"] = 9
}
},
["pos"] = "lol"
table.tofile
This will save tables as they are in a file.
PHP:
t= {pos="lol", {pos={x=2,y=2,z=9}}}
x=table.totext(t, "table_file", "table")
Code:
table = {
[1] = {
["pos"] = {
["y"] = 2,
["x"] = 2,
["z"] = 9
}
},
["pos"] = "lol"
}
Enjoy,
Colandus
Last edited: