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

question about why my code has logic errors

Started by EveryOS, 18 April 2016 - 08:02 PM
EveryOS #1
Posted 18 April 2016 - 10:02 PM
I have some code:

function replace(string, s, v)
  string = tostring(string)
  local t={}; i =1
  for str in string.gmatch(string, "([^"..s.."]+)") do
   table.insert(t, str)
  end
  string = t[1]
  for k, x in ipairs(t) do
   if k~=1 then
	 string = string..v..x
   end
  end
  return string
end
print(replace(':/NN/print/NN','"NyanOS"','"..OS.."'))

But it keeps replacing 'n' from 'print' with 'NN'
Also, it leaves out the last 'NN'

Edit: oopsies. Put print(replace(':/NN/print/NN','"NyanOS"','"..OS.."'))
instead of :
print(replace(':/NN/print/NN','"NN"','"..OS.."'))
in my editing.
Edited on 19 April 2016 - 10:53 AM
KingofGamesYami #2
Posted 18 April 2016 - 10:15 PM
What are you trying to achieve with this code?
Bomb Bloke #3
Posted 19 April 2016 - 12:53 AM
I get the impression he's trying to re-invent string.gsub():

print(string.gsub(":/NyanOS/print/NyanOS","NyanOS",OS))
EveryOS #4
Posted 19 April 2016 - 02:01 AM
I get the impression he's trying to re-invent string.gsub():

print(string.gsub(":/NyanOS/print/NyanOS","NyanOS",OS))
THX