Page 1 of 1

need help with for .... in pairs

Posted: Sun Jun 13, 2021 12:11 am
by Ketlambek
Hi Guys! Sorry, I'm bad in English.

I'm trying to change the stack size for some items. I have this code:

Code: Select all

--data-final-fixes.lua
local items = data.raw.item

function get_stack_size(str)
	-- if the specified name has such a substring, then return the specified stack size
	if str:find("transport-belt") ~= nil then return settings.startup["Ket-transport-belt-stack-size"].value end
	return nil
end

local size = nil
for i, e in pairs(items) do
	size = get_stack_size(e.name)
	if size ~= nil then
		e.stack_size = size
		size = nil
	end
end
but nothing change... It seems to me that in "e" is not an item structure and in "i" is not an index of items array. Or is the problem something else?

UPD: I understand that problem is not with "e" and "i". If I use item internal name in condition operator then stack size is already changes. Why it doesn't works with finding a substring?

Re: need help with for .... in pairs

Posted: Sun Jun 13, 2021 1:22 pm
by DaveMcW
https://www.lua.org/manual/5.2/manual.html#6.4.1 wrote:The following combinations are allowed in describing a character class:
x: where x is not one of the magic characters ^$()%.[]*+-?
%x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters.
To search for - in a string, you need to escape it.

Code: Select all

str:find("transport%-belt")

Re: need help with for .... in pairs

Posted: Sun Jun 13, 2021 10:04 pm
by eradicator
Or use plaintext find mode.

Code: Select all

str:find("transport-belt", 1, true)

Re: need help with for .... in pairs

Posted: Tue Jun 15, 2021 4:16 am
by Ketlambek
thanks everyone for solutions! I use eradicator's method before reading his reply.

Re: need help with for .... in pairs

Posted: Tue Jul 13, 2021 12:12 pm
by Mernom
You could also iterate over all transport-belt type entities and find their item-to-place-this, no?

Re: need help with for .... in pairs

Posted: Mon Aug 16, 2021 6:05 pm
by Ketlambek
Then I did so, and then I abandoned the mod...