• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Table Functions

Evil Hero

Legacy Member
TFS Developer
Joined
Dec 12, 2007
Messages
1,254
Solutions
27
Reaction score
721
Location
Germany
Here are some table functions which I think that they are really usefull.

Lua:
table.getIndexOfLowest = function(table)
local lowest, r = table[#table], {}
	for var = #table,1, -1 do
		if table[var] == lowest then
			table.insert(r,var)
			lowest = table[var]
		elseif table[var] < lowest then
			for a = 1,#r do
				table.remove(r, a)
			end
			table.insert(r, var)
			lowest = table[var]
		end
	end
	return r
end
example:
Lua:
local t = {5,3,1,2,7}
if #table.getIndexOfLowest(t) > 1 then
	for a = 1,#table.getIndexOfLowest(t) do
		print(table.getIndexOfLowest(t)[a])
	end
else
	print(table.getIndexOfLowest(t)[1])
end

output-> 3

because in the table the index of 3 has the lowest value.

if we however have in a table 2 times the same value like this
local t = {5,3,1,2,7,1}

output-> 3
output-> 6

works same as above but returns the index of the highest value.
Lua:
table.getIndexOfHighest = function(table)
local highest, r = table[#table], {}
	for var = #table,1, -1 do
		if table[var] == highest then
			table.insert(r,var)
			highest = table[var]
		elseif table[var] > highest then
			for a = 1,#r do
				table.remove(r, a)
			end
			table.insert(r, var)
			highest = table[var]
		end
	end
	return r
end

Lua:
table.shuffle = function(table)
local iterations = #table
	for i = iterations, 2, -1 do
		j = math.random(i)
		table[i], table[j] = table[j], table[i]
	end
end

example:
Lua:
local t = {nil,true,false,50,"hello"}
math.randomseed(os.time())
table.shuffle(t)

output-> {true,"hello",false,nil,50}

basicly it just randomize the index with it's according value to another index.


If you have any kind of requests for table functions feel free to ask here, will try to add them then.



Kind regards, Evil Hero.
 
Last edited:
good job bro, really useful ;) also you can explain all the others table functions like find, count, combinations, etc..
good work anyways :)
 
or u could just sort it and take first element out :p (i dont remember if u get same keys after sorting though....)
 
or u could just sort it and take first element out :p (i dont remember if u get same keys after sorting though....)
dunno havn't tried tbh, thought it was more like my shuffle function where it just changes the value to a new index, but it's worth a shot for sure.

btw, I had a slight error on the shuffle function, look at the example again how to use it, you have to move the randomseed out of the function so it does provide a real random.
 
Back
Top