Page 6 of 13

Re: Small Useful Functions

Posted: Wed May 28, 2014 9:28 am
by Robin
It also might not add them in the order you want to -- if you want the values from the other table added in order, use ipairs.

Re: Small Useful Functions

Posted: Wed May 28, 2014 4:57 pm
by szmol96
Gotcha.

Re: Small Useful Functions

Posted: Tue Sep 30, 2014 4:50 pm
by Doctory

Code: Select all

requirei = local function(d, ...)
	args = {...}
	for k, i in ipairs(args) do
		file = d .. i
		require(file)
	end	
end

Re: Small Useful Functions

Posted: Tue Sep 30, 2014 10:26 pm
by Robin
Or:

Code: Select all

local function requirei(d, ...)
	for i, v in ipairs{...} do
		require(d .. v)
	end	
end
,

which has the advantage of not overwriting or leaking the globals args and file. Also this doesn't contain a syntax error on the first line.

Re: Small Useful Functions

Posted: Wed Oct 01, 2014 12:43 pm
by Doctory
i suppose this would be helpful as well:

Code: Select all

local function clear(t)
	for i,_ in ipairs(t) do
		table.remove(t, i)
	end
end

Re: Small Useful Functions

Posted: Wed Oct 01, 2014 1:26 pm
by Robin
That function is both ineffective and inefficient: it only removes half of the items, and it takes 4 times as long for a table that's 2 times as big.

Better would be:

Code: Select all

local function clear(t)
	for i = #t, 1, -1 do
		t[i] = nil
	end
end
This removes all the items, without shuffling them around.

Re: Small Useful Functions

Posted: Wed Oct 01, 2014 11:53 pm
by davisdude
Hopefully there's nothing wrong with this one (just to keep Robin's job a little easier ;))

Code: Select all

local function RemoveHole( Table ) -- Removes nils from tables.
	local New = {}

	for Index, Value in pairs( Table ) do
		New[Index] = Value
	end
	
	return New
end

Re: Small Useful Functions

Posted: Thu Oct 02, 2014 6:01 am
by micha
Robin wrote:Better would be:

Code: Select all

local function clear(t)
	for i = #t, 1, -1 do
		t[i] = nil
	end
end
If you remove all of the entries, then you don't even need to traverse the table backwards. This works, too:

Code: Select all

local function clear(t)
	for i = 1,#t do
		t[i] = nil
	end
end
davisdude wrote:Hopefully there's nothing wrong with this one (just to keep Robin's job a little easier ;))

Code: Select all

- snip -
What exactly should this function do? What do you mean by "remove nils values?" A variable with value "nil" does not exist. So if you have a nil-value in a table then the key-value pair does not exist. Assigning nil to a variable is a way to delete variable.

Re: Small Useful Functions

Posted: Thu Oct 02, 2014 10:54 pm
by davisdude
Ya know, I'm not even sure what I was thinking when I wrote that.

Re: Small Useful Functions

Posted: Mon Oct 06, 2014 4:36 pm
by Sapper
Heres a few, kinda useful for tetris type games and map generating

new array

Code: Select all

function newAr(h,w)
	local ar = {}
	for i=1,h do
		ar[i] = {}
		for j=1,w do
			ar[i][j] = 0
		end
	end
	return ar
end
transpose array

Code: Select all

function transpose(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[j][i] = ar[i][j]
		end
	end
	return ar2
end
flip array

Code: Select all

function vFlip(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[#ar - i + 1][j] = ar[i][j]
		end
	end
	return ar2
end

function hFlip(ar)
	local ar2 = newAr(#ar,#ar[1])
	for i=1,#ar do
		for j=1,#ar[i] do
			ar2[i][#ar[i]-j + 1] = ar[i][j]
		end
	end
	return ar2
end
and then rotate array

Code: Select all

function rotLeft(ar)
    return vFlip(transpose(ar))
end

function rotRight(ar)
    return hFlip(transpose(ar))
end