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

Help, why doesn't this print out results?

Started by evanbrown24, 07 April 2013 - 12:35 PM
evanbrown24 #1
Posted 07 April 2013 - 02:35 PM
I'm trying to make an extremely basic tool so I can leave messages to my friend, because he usually plays while i'm at sports so we need to tell what we did. Heres the code for the adding messages program:

term.clear()
term.setCursorPos(1,1)
print("Please enter message below:")
input = read()
input = messages
file = fs.open("msgconfig", "a")
file.writeLine(messages)
file.close()
print("message saved!")

Heres the code for my program that gives you the messages
file = fs.open("msgconfig", "r")
message = file.readAll()
print(message)
file.close()
if message == nil then
print("No message found")
end

Whenever I run the program that prints the messages out, all it returns is several blank lines. It doesn't return a "no message found", so that means that there's some data thats getting added to it. also, sorry for the messy code. Idk how to format code the "pro" way, I just keep it all straight. Please give me a link also on how to make your code easier for the eyes if you can :P/>
SuicidalSTDz #2
Posted 07 April 2013 - 02:41 PM
term.clear()
term.setCursorPos(1,1)
print("Please enter message below:")
local input = read()
local file = fs.open("msgconfig", "a")
file.writeLine(input)
file.close()
print("message saved!")

And..


local file = fs.open("msgconfig", "r")
local message = file.readAll()
if message == nil then
 print("No message found")
else
 print(message)
 file.close()
end

Use local variables unless they need to be used outside the block/chunk they are defined in

This code is also not tested. Come back if it does not work and we will go from there.
Engineer #3
Posted 07 April 2013 - 02:48 PM
term.clear()
term.setCursorPos(1,1)
print("Please enter message below:")
local input = read()
local file = fs.open("msgconfig", "a")
file.writeLine(input)
file.close()
print("message saved!")

And..


local file = fs.open("msgconfig", "r")
local message = file.readAll()
if message == nil then
 print("No message found")
else
 print(message)
 file.close()
end

Use local variables unless they need to be used outside the block/chunk they are defined in

This code is also not tested. Come back if it does not work and we will go from there.
I would rather use this for reading:

local handle = fs.open('msgconfig', 'r')
local lines = {}
while true do
local line = handle.readLine()
if not line then break end
table.insert(lines, line)
sleep(0)
end
handle.close()

print('the last message was;\n' .. lines[#lines])

:)/>/>
evanbrown24 #4
Posted 07 April 2013 - 02:51 PM
term.clear()
term.setCursorPos(1,1)
print("Please enter message below:")
local input = read()
local file = fs.open("msgconfig", "a")
file.writeLine(input)
file.close()
print("message saved!")

And..


local file = fs.open("msgconfig", "r")
local message = file.readAll()
if message == nil then
print("No message found")
else
print(message)
file.close()
end

Use local variables unless they need to be used outside the block/chunk they are defined in

This code is also not tested. Come back if it does not work and we will go from there.

It worked! :D/> Thanks so much! I've only coded in html so lua is like learning how to program all over. I thought that all variables were local by default, and global if defined specifically.
SuicidalSTDz #5
Posted 07 April 2013 - 02:51 PM
local lines = handle.readLine()
for lines in handle.readLine do
 table.insert(foo, foo)
end

And I reject your loop and substitute my iterator :P/>

I was also just using the code he supplied :P/>

EDIT: No problem. Also, in Lua, variables are defined global unless declared otherwise, which I like :D/>
Engineer #6
Posted 07 April 2013 - 02:53 PM
local lines = handle.readLine()

for lines in handle.readLine do
table.insert(foo, foo)
end

And I reject your loop and substitute my iterator :P/>/>

I was also just using the code he supplied :P/>/>
I saw somewhere that loop, but couldnt remember it. Thats why I used thr 'lazy' way.

Well, both are possible, its up to the OP wich he chooses
SuicidalSTDz #7
Posted 07 April 2013 - 02:55 PM
local lines = handle.readLine()

for lines in handle.readLine do
table.insert(foo, foo)
end

And I reject your loop and substitute my iterator :P/>/>

I was also just using the code he supplied :P/>/>
I saw somewhere that loop, but couldnt remember it. Thats why I used thr 'lazy' way.

Well, both are possible, its up to the OP wich he chooses
This loop is just cleaner IMO. It is totally up to him, however, in the current state it will return nil on the first line :P/>

EDIT: I also like to use os.pullEvent as an iterator <3

for event, p1, p2 in os.pullEvent do
 print(event..""..p1..""..p2)
end