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

My Document Program

Started by how2doit, 25 January 2016 - 09:15 PM
how2doit #1
Posted 25 January 2016 - 10:15 PM
I'm in need of help, I've made this program that will let me write documents in a computer craft computer, but i have three problems 1 I cannot figure out how to get it to ask if you want to overwrite something if something already exists, number 2 I have forgoten now to "fix" write functions with multiple statement options, and number 3 i cannot figure out how to get it to store the data stored in my table after I've exited the computer if you can help I'd appreciate it. :D/>
Spoilerlocal log = {"l1","l2","l3","l4","l5","l6","l7","l8","l9","l10"}
os.pullEventRaw()
while true do
write("Exit, retrive, or write?:")
if read() == "retrive" then
term.write("Which?, logs l-10:")
local z = read()
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint(log[z], 8)
sleep(1)
elseif read() == "write"
then
local w
write("Which log?, 1-10:")
w = read()
write("Log goes here:")
log[w] = read()
elseif read() == "exit" then
break
end
end
end
Lyqyd #2
Posted 25 January 2016 - 10:48 PM
Moved to Ask a Pro.
Bomb Bloke #3
Posted 26 January 2016 - 12:17 AM
1 I cannot figure out how to get it to ask if you want to overwrite something if something already exists

Assuming you mean a file, use fs.exists().

number 2 I have forgoten now to "fix" write functions with multiple statement options

What?

number 3 i cannot figure out how to get it to store the data stored in my table after I've exited the computer

Use fs.open() alongside textutils.serialise(). Eg:

-- Save table to file:
local output = fs.open("someFile.txt", "w")
output.writeLine(textutils.serialise(someTable))
output.close()

-- Load table from file:
local input = fs.open("someFile.txt", "r")
local someTable = textutils.unserialise(input.readAll())
input.close()
how2doit #4
Posted 26 January 2016 - 12:30 AM
Thanks and
what i ment by the second point is the first option that gets checked while reading my code only has to be typed once but if wanted to write a file i need to type it in twice and so on.
Dragon53535 #5
Posted 26 January 2016 - 06:02 AM
Still confused, can you give us an example?
how2doit #6
Posted 26 January 2016 - 10:00 PM
okay, in this code if i want to write i'd say write then it just skips down a line and i have to type it again. if you are still confused just run my code and you'll find out what i mean
Edited on 26 January 2016 - 09:04 PM
valithor #7
Posted 26 January 2016 - 10:55 PM
okay, in this code if i want to write i'd say write then it just skips down a line and i have to type it again. if you are still confused just run my code and you'll find out what i mean

That behavior is coming from how you used the read function. You are calling the read function in each of your if statements, which is telling the computer to ask for more input from the user. You only want to ask for input once, so set a variable to what read returns before the if statements, and then check what the variable equals in them.


while true do
  local input = read() --# getting input from user via read
  if input == "hello" then --# comparing against the variable
    print("hi")
  elseif input == "bye" then --# comparing against the variable
    print("bye")
  end
end

You only need to change the reads that are part of the if statements to where they use the variable. Any others are fine.
how2doit #8
Posted 27 January 2016 - 12:14 AM
Oh i get it better now okay