Jump to content




Fs.writeline() And "quotes"

lua

  • You cannot reply to this topic
7 replies to this topic

#1 Andale

  • Members
  • 46 posts
  • LocationNWIndiana

Posted 23 January 2013 - 08:18 PM

Let's say I want to create a file to run from based on things done in the program I'm currently running. I understand the tables and reading and writing with files pretty decently, learned that back in the qBasic days (which does make it harder sometimes).
Today's issue? I want to create a file that is a working program. So I'm trying to insert lines like:
if answer == "yes" then
print("maybe")
else
print("probably not")
end

fs.writeLine and fs.write seem ok with inputting data values
fs.writeLine(themagicnumber)
fs.writeLine("The magic words")
but how can I write to file the first code since it has quotations in it already?
It expects a ) after the " so I only get as far as the first variable.
If I were not setting up commands that REQUIRE quotes it wouldn't be so bad.

For my purposes I could bypass this by using fs.move to rename the file and adding some variables somehow, but I don't see a way for a program to get it's own name, and I cna't assume people would not rename it when (more like if) downloading.

Also, I found this thread which is helpful for those learning file manipulation. It didn't have an answer and was old so I made a new one.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 January 2013 - 08:21 PM

you can escape the quote with \ so it would look like this

print("\"maybe\"")

EDIT: Optionally you can also use string.format ... example

string.format( "The following will have quotes %q", "Hello" )

read more here http://lua-users.org...LibraryTutorial under string.format

Edited by TheOriginalBIT, 23 January 2013 - 08:24 PM.


#3 Zoinky

  • Members
  • 144 posts
  • LocationWellington, New Zealand

Posted 23 January 2013 - 08:26 PM

You can also use apostrophes instead of quotes. E.g. print(' "Maybe" ')

EDIT:

View PostAndale, on 23 January 2013 - 08:18 PM, said:

I don't see a way for a program to get it's own name

shell.getRunningProgram()

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 January 2013 - 08:27 PM

View PostZoinky, on 23 January 2013 - 08:26 PM, said:

You can also use apostrophes instead of quotes. E.g. print(' "Maybe" ')
Or the other way around
print( "'Maybe'" )

#5 Andale

  • Members
  • 46 posts
  • LocationNWIndiana

Posted 23 January 2013 - 08:33 PM

like i said before, best support forum ever
thanks much guys

#6 Andale

  • Members
  • 46 posts
  • LocationNWIndiana

Posted 24 January 2013 - 05:07 AM

gonna try to post it here in a current related thread first

Can anyone see why this is not even creating a file when I can replicate this line for line in the LUA prompt and it works fine?

local tArgs = {...}
local code, filename = tArgs[2], tArgs[1]
if #tArgs ~= 2 then
print ("Usage: <thisfile> <PasteBINcode> /<filename_to_use>")
sleep (2)
print ("That's ok, we can do it now.")
sleep (1)
print ("What is your pastebin code? ")
print ("(Example: pastebin.com/GvwZ4r is GvwZ4r)")
code = read()
if code == "2piRGMv2" then
  repeat
   print ("No. That's this program. Try again!")
   code = read()
  until code ~= "2piRGMv2"
end
print ("What name do you want for your program?")
print ("(include /disk/ if used, works in prompt too)")
filename = read()
if filename == "startup" or filename == "Startup" then
  repeat
   print ("This program will create a startup file. Choose another!")
   filename = read()
  until filename ~= "startup"
end
end

while filename == "startup" do
print ("This program will create a startup file. Choose another.")
filename = read ()
end
while code == "2piRGMv2" do
print ("No. That's this program.")
code = read()
end
-- ADD A PULL EVENT TO WARN/CANCEL DELETING STARTUP
stop=5
repeat
term.clear()
term.setCursorPos(1,1)
print("This WILL delete your current 'startup' file.")
print("Afterwards it will delete itself.")
print("Press CTRL+T to STOP!")
write (stop .. "...")
stop=stop-1
sleep(.8)
until stop == 0
print("Deleting most likely copies of startup")
fs.delete("startup")
fs.delete("Startup")
fs.delete("StartUp")
fs.delete("StartUP")
fs.delete("STARTUP")
su=fs.open("startup", "w")
print ("opening file")
print("write")
--su.writeLine=("filname = " .. filename)
--su.writeline=("code = " .. code)
su.writeLine=("print (\"Deleting current copy!\")")
su.writeLine=('shell.run ("delete", filename)')
su.writeLine=('sleep (0.1)')
su.writeLine=('print ("Downloading from pastebin...")')
su.writeLine=('shell.run ("/rom/programs/http/pastebin", "get", code, filename)')
su.writeLine=('print ("Done!")')
su.writeLine=('sleep (0.1)')
su.writeLine=('print ("Running the file..." .. filename)')
su.writeLine=('sleep (1)')
su.writeLine=('shell.run (filename)')
print("closing file")
su.close()
sleep(1)
-- del = shell.getRunningProgram()
-- fs.delete(del)
-- os.reboot()
return


#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 January 2013 - 06:21 AM

su.writeLine() instead of su.writeLine=(). You're supposed to be calling the function, not changing the value. Also, block quotes may be easier to use.

su.write([[print("bob")
for i =1,3 do
print(i)
end]])

That would write all for of those lines (completely correctly!) to the file.

#8 Andale

  • Members
  • 46 posts
  • LocationNWIndiana

Posted 24 January 2013 - 08:49 AM

i like ho i didn't see the equals sign in there when i typed it directly into prompt.
it's weird what u miss sometimes.


I didn't know about block quotes, thank u much, those are gonna come in super handy l8r





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users