Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 808 Bytes

3.2.md

File metadata and controls

40 lines (32 loc) · 808 Bytes

lua Table lib

insert and delete

table.insert(t, 1, 5) --> {1,2,3} => {5,1,2,3}

t = {}
for line in io.lines() do
	table.insert(t, line) --> append line to ending of t
	--> but t[#t+1] = line is recommended in lua5.1
end
print (#t)

table.delete() will delete the last item in array t by default.

table.insert() and table.delete() can be used to simulate stack.

t = {} --> initialize stack
table.insert(t, xx) --> push
table.delete(t) --> pop

sort

ipairs uses array order, while pairs uses table's order.

concat

this extended (and recursive) version of concat is able to handle nested table.

function rconcat(l)
	if type(l) ~= "table" then return l end
	local res = {}
	for i = 1, #l do
		res[i] = rconcat(l[i])
	end
	return table.concat(res)
end