Forum

> > CS2D > Scripts > Small Useful Functions
Forums overviewCS2D overview Scripts overviewLog in to reply

English Small Useful Functions

3 replies
To the start Previous 1 Next To the start

moved Small Useful Functions

Dousea
User Off Offline

Quote
I created this thread because inspired by a thread in LÖVE forums. But let me get this straight, these are small but really useful functions to make you easy at scripting. For now I only show small useful table functions.

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
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
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
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
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

old Re: Small Useful Functions

_Yank
User Off Offline

Quote
math.round(value)
Rounds a rational number.
1
2
3
4
function math.round(value)
    if value <= math.floor(value) + .5 then return math.floor(value)
    else return math.ceil(value) end
end

old Re: Small Useful Functions

Dousea
User Off Offline

Quote
string.indexof(string, characters, fromindex)
Searches for characters inside a string and returns the start and end index position of the characters.
1
2
3
4
5
6
7
8
9
10
11
function string.indexof (text, character, fromindex)
	local fromindex = fromindex or 1
	
	for index = fromindex, #text do
		if (text:sub (index, index + #character - 1) == character) then
			return index + 1 - fromindex, (index + 1 - fromindex) + #character - 1
		end
	end
	
	return 0, 0
end

string.lastindexof(string, characters, fromindex)
Searches for characters inside a string, working backward from the last character, and returns the start and end index position of the matching characters
1
2
3
4
5
6
7
8
9
10
11
function string.lastindexof (text, character, fromindex)
	local fromindex = fromindex or #text
	
	for index = fromindex, 1, -1 do
		if (text:sub (index - #character + 1, index) == character) then
			return #text + 1 - index, (#text + 1 - index) + #character - 1
		end
	end
	
	return 0, 0
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview