table.indexof(table, value, fromindex)
Searches for a value in a table and returns the index position of the value.
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
function table.indexof(list, value, fromindex) 	local fromindex = fromindex or 1 	 	for index = fromindex, #list do 		if (list[index] == value) then 			return index + 1 - fromindex 		end 	end 	 	return 0 end
table.lastindexof(table, value, fromindex)
Searches for a value in a table, working backward from the last value, and returns the index position of the matching value.
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
function table.lastindexof(list, value, fromindex) 	local fromindex = fromindex or #list 	 	for index = fromindex, 1, -1 do 		if (list[index] == value) then 			return #list + 1 - index - fromindex 		end 	end 	 	return 0 end
table.sorton(table, index, function)
Sorts a table by value in index.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function table.sorton(list, sortindex, func) 	local transformedlist = {} 	local newlist = {} 	 	for index, value in pairs(list) do 		table.insert(newlist, value[sortindex]) 	end 	 	table.sort(newlist, func) 	 	for newindex, newvalue in pairs(newlist) do 		for index, value in pairs(list) do 			if (value[sortindex] == newvalue) then 				table.insert(transformedlist, value) 			end 		end 	end 	 	for index = 1, #list do 		list[index] = nil 	end 	 	for index, value in pairs(transformedlist) do 		table.insert(list, value) 	end end
table.slice(table, startindex, endindex)
Returns a new table that consists of a range of values from the original table.
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
function table.slice(list, startindex, endindex) 	local newlist = {} 	 	for index = startindex, endindex do 		table.insert(newlist, list[index]) 	end 	 	return newlist end
If you want to add more small-but-useful functions, feel free to comment.
edited 1×, last 07.10.14 12:49:37 pm