This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
roger109z's profile picture

change a specific part of string? and turning a string into a table

Started by roger109z, 27 May 2016 - 01:10 AM
roger109z #1
Posted 27 May 2016 - 03:10 AM
is there a way to change a certain part of a code? like per say if I had:


map = {
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
"44444444444444444444444444444444444444444444444444",
}
and I wanted to change the "0" at (10, 11) to a "4" how would I go about doing that?
also if I wanted to change one of the strings into a table to where I have every character as a different table entry how would I go about doing that?
TYKUHN2 #2
Posted 27 May 2016 - 03:21 AM

function modAt(x, y, new)
	map[x] = map[x]:sub(1, y-1)..new..map[x]:sub(y+1)
end

function split(str)
	temp = {}
	for i = 1, str:len() do
		temp:insert(str:sub(i, i))
	end
	return temp
end

Not guaranteed to work (especially that temp:insert I haven't tried that one yet)
roger109z #3
Posted 27 May 2016 - 03:30 AM

function modAt(x, y, new)
	map[x] = map[x]:sub(1, y-1)..new..map[x]:sub(y+1)
end

function split(str)
	temp = {}
	for i = 1, str:len() do
		temp:insert(str:sub(i, i))
	end
	return temp
end

Not guaranteed to work (especially that temp:insert I haven't tried that one yet)

Thank you so much!
Bomb Bloke #4
Posted 27 May 2016 - 03:34 AM
(especially that temp:insert I haven't tried that one yet)

Even if that were a thing, it'd be faster to avoid the excess function calls and do:

local function split(str)
        local temp = {}
        for i = 1, #str do
                temp[i] = str:sub(i, i)
        end
        return temp
end