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

Converting text in a file

Started by Nothy, 12 July 2016 - 10:41 PM
Nothy #1
Posted 13 July 2016 - 12:41 AM
Okay, getting bored is apparently very unhealthy for me.

Anyway, what I'm trying to do is that I have a file with just random words, one per line.

potato
carrot
tomato
--#And so on

And I want to quote every word and add a comma afterwards.

So far I have this code:
(Yes this is by far not enough)

function convert()
  local file = fs.open("conversion.txt", "r");
  local finalconversion = fs.open("converted.txt","w")
  local arr = {}
  for line in io.lines(file) do
	 finalconversion.writeLine(' "'..line..'", ')
	 print('"'..line..'", ')
  end
  file.close()
  finalconversion.close()
end
convert()

Nothy #2
Posted 13 July 2016 - 01:11 AM
Got it working.


function convert()
  local file = io.open("convert.txt", "r");
  local f = io.open("converted.txt","w")
  local arr = {}
  for line in file:lines(file) do
	 f:write('"'..line..'", \n')
	 print('"'..line..'", \n')
	 sleep(0.05)
  end
  file:close()
  f:close()
end
convert()

Bomb Bloke #3
Posted 13 July 2016 - 01:12 AM
You forgot to mention your problem, but other than the bit where you call io.lines() with a table instead of a string, that code looks more or less acceptable to me.

local function convert()
  local finalconversion = fs.open("converted.txt","w")
  for line in io.lines("conversion.txt") do
         finalconversion.writeLine("\""..line.."\", ")
         print("\""..line.."\", ")
  end
  finalconversion.close()
end

convert()

If you're still having issues, remember to elaborate on what they are.
Lyqyd #4
Posted 13 July 2016 - 01:29 AM
He's not calling io.lines, he's calling handle:lines() on a handle opened with io.open. The handle:lines() call does not require an argument, though.
MKlegoman357 #5
Posted 13 July 2016 - 10:42 AM
He's not calling io.lines, he's calling handle:lines() on a handle opened with io.open. The handle:lines() call does not require an argument, though.

He is calling io.lines() in the first post.
Bomb Bloke #6
Posted 13 July 2016 - 11:09 AM
In his second post, yes - I got ninja'd by that one.
Nothy #7
Posted 13 July 2016 - 04:31 PM
Thank you all for the responses :)/> I did get it to work flawlessly