Я люблю Lua. I love Lua.

Сравнение таблиц Lua

Posted in Uncategorized by ilovelua on Апрель 1, 2013

Есть две таблицы вида:

[1] = "{6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}",
[3] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
...

и нужно их мне сравнить. Сравнение я сделал двумя циклами:

function areTablesEqual1(table1, table2)
	for k, v in pairs(table1) do
		if table2[k] ~= v then
			return false
		end
	end

	for k, v in pairs(table2) do
		if table1[k] ~= v then
			return false
		end
	end

	return true
end

Проверим:

local table1 = {	
	[1] = "{6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}",
	[3] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[5] = "{69DC8AE7-8F77-427B-B8AA-B19D3F478B66}",
	[7] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[9] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[11] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[13] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[15] = "{DB434044-F5D0-4F1F-9BA9-B73027E18DD3}",
	[17] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[19] = "{69DC8AE7-8F77-427B-B8AA-B19D3F478B66}",
}

local table2 = {	
	[1] = "{6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}",
	[3] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[5] = "{69DC8AE7-8F77-427B-B8AA-B19D3F478B66}",
	[7] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[9] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[11] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[13] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[15] = "{DB434044-F5D0-4F1F-9BA9-B73027E18DD3}",
	[17] = "{BCE4E030-38E9-423E-98ED-24BE3DA87C32}",
	[19] = "{69DC8AE7-8F77-427B-B8AA-B19D3F478B66}",
}
assert(areTablesEqual1(table1, table1))
assert(areTablesEqual1(table1, table2))

И вдруг отчего-то захотелось мне сравнить их внутри одного цикла, а не двух. Для этого на помощь призвал я функцию next():

function areTablesEqual2(table1, table2)
	local result = true

	local key1, value1 = next(table1)
	local key2, value2 = next(table2)

	while key1 and key2 do
		if key1 ~= key2 or value1 ~= value2 then
			result = false
			break
		end

		key1, value1 = next(table1, key1)
		key2, value2 = next(table2, key2)
	end

	if result then
		result = (not key1) and (not key2)
	end

	return result
end

Проверим:

assert(areTablesEqual2(table1, table1))
assert(areTablesEqual2(table1, table2)) -- <-- fail!

Посмотрим, что там происходит внутри:

function areTablesEqual2(table1, table2)
	print('areTablesEqual2 output:')
	local result = true

	local key1, value1 = next(table1)
	local key2, value2 = next(table2)

	print('~~~table1', key1, value1)
	print('~~~table2', key2, value2)

	while key1 and key2 do
		if key1 ~= key2 or value1 ~= value2 then
			result = false
			break
		end

		key1, value1 = next(table1, key1)
		key2, value2 = next(table2, key2)

		print('~~~table1', key1, value1)
		print('~~~table2', key2, value2)
	end

	if result then
		result = (not key1) and (not key2)
	end

	return result
end

Вывод:
areTablesEqual2 output:
~~~table1 13 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 13 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 7 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 7 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 1 {6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}
~~~table2 1 {6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}
~~~table1 15 {DB434044-F5D0-4F1F-9BA9-B73027E18DD3}
~~~table2 15 {DB434044-F5D0-4F1F-9BA9-B73027E18DD3}
~~~table1 9 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 9 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 5 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table2 5 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table1 19 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table2 19 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table1 3 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 3 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 17 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 17 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 11 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 11 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 nil nil
~~~table2 nil nil
areTablesEqual2 output:
~~~table1 13 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 13 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 7 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 7 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 1 {6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}
~~~table2 1 {6D21ECEA-F85B-4E8D-9D51-31DC9B8AA4EF}
~~~table1 15 {DB434044-F5D0-4F1F-9BA9-B73027E18DD3}
~~~table2 15 {DB434044-F5D0-4F1F-9BA9-B73027E18DD3}
~~~table1 9 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table2 9 {BCE4E030-38E9-423E-98ED-24BE3DA87C32}
~~~table1 5 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table2 5 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table1 19 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table2 19 {69DC8AE7-8F77-427B-B8AA-B19D3F478B66}
~~~table1 3 {BCE4E030-38E9-423E-98ED-24BE3DA87C32} <
~~~table2 17 {BCE4E030-38E9-423E-98ED-24BE3DA87C32} <

Ну вот, различный порядок обхода таблиц. Жаль, что не получилось. А идея была неплохая, да…

Реклама

Удаление данных из таблицы внутри цикла

Posted in Uncategorized by ilovelua on Октябрь 25, 2011

Есть массив:

local t = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Из него нужно удалить все элементы большие 2 и меньшие 9.

Неправильное решение: (more…)

Tagged with: , , , , ,