Jump to content




Converting text in a file


6 replies to this topic

#1 Nothy

  • Members
  • 249 posts
  • LocationMars

Posted 12 July 2016 - 11:41 PM

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()



#2 Nothy

  • Members
  • 249 posts
  • LocationMars

Posted 13 July 2016 - 12: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()



#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 July 2016 - 12: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.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,462 posts

Posted 13 July 2016 - 12: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.

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 13 July 2016 - 09:42 AM

View PostLyqyd, on 13 July 2016 - 12:29 AM, said:

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.

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 July 2016 - 10:09 AM

In his second post, yes - I got ninja'd by that one.

#7 Nothy

  • Members
  • 249 posts
  • LocationMars

Posted 13 July 2016 - 03:31 PM

Thank you all for the responses :) I did get it to work flawlessly





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users