7 posts
Posted 30 June 2013 - 10:22 PM
hello , so im really confused , so here are the details :
what im having trouble with is that , im trying to make a list program , now , the program basicly waits for user's "input"/what he has typed , (now heres where i dont know what to do) , then i want it to store it in a variable , i can do this but the problem is that i dont want to manually add variables and type in the command : "variable = io.read" tthen all the conditional statements bla bla . . . i want it to make a new variable within the program itself and store that data which user typed for that variable , so its basicly like i guess : a program that makes a new variable within itself and its data is what the user typed [name etc] . . . i hope you understand , if you didnt , i can put it in more detail , thanks :)/> !
25 posts
Posted 30 June 2013 - 11:05 PM
You could maybe create a variable then set that variable to what they input and then store it into a table
something like
local mytable = {}
local userinput = ""
write("enter stuff: ")
userinput == read()
table.insert(mytable, userinput)
191 posts
Posted 01 July 2013 - 04:03 AM
That works, but if he only wanted to store one value he wouldnt have created this thread.
Local table={}
for i =1,amountofnumberstostore do
print("Enter data:")
table =read()
End
Capitalization errors mugt have vame in there, im on iphone
159 posts
Location
A Chair
Posted 01 July 2013 - 11:01 AM
If I understand you correctly… Every time your user enters data you want the data stored in a new Varible…
The problem with this is on reboot you will lose all data… It might be best to save the data to a file, for example you write all your data into a table. Serialize the data then save into the file.
When the data is wanted to be listed, you read the file back and unserialize the data back into a table and then use a "for 1, #tablename do" and print the results.
when the server is accidentally restarted all your data is still in the file and loads as normal.
Could you post your current code that you are using so we know the function / layout of the program please, this would help us greatly.
btw. You can use pastebin.com if your code is long, This has a great feature of you can identify your code as Lua… (formats the code for easier reading)
Edit to add code…
local tableTop = { "" }
local cmpid = os.getComputerID()
while true do
-- now we are looping
term.clear()
term.setCursorPos(1,1)
Print("computer ID - " .. cmpid)
Print()
print("Please choose an option")
print(" 1. Input Data")
print(" 2. List Data")
print(" 3. Shutdown")
print()
write(" : ")
local input = read()
if input == "3" then
os.shutdown()
elseif input == "2" then
-- code for listing data
elseif input == "1" then
-- Code for Inputting data
else
print("Command not recognised...")
sleep(2)
end
end
Edited on 01 July 2013 - 09:20 AM
159 posts
Location
A Chair
Posted 01 July 2013 - 02:21 PM
I was playing around with the ideas a little and wrote this. I hope it can help you with your problems and hopefully allow you to save the data to a file
without too much trouble.
local filename = "datalist" -- set you filename to datalist
local tableTop = { "" } -- Make sure the table is clear and defined
local cmpid = os.getComputerID() -- Part of all my programs - comp ID for networking
-- function section
-- Main Program section
while true do
-- now we are looping
term.clear()
term.setCursorPos(1,1)
Print("computer ID - " .. cmpid) -- part of all my programs Id the comp
Print()
print("Please choose an option")
print(" 1. Input Data")
print(" 2. List Data")
print(" 3. Shutdown")
print()
write(" : ")
local input = read()
if input == "3" then
os.shutdown()
elseif input == "2" then
-- code for listing data
---[[ Filename - opening tutorial
-- this commented code can be disabled by one - char.
if fs.exists(filename) == true then
file = io.open(filename, "r")
local i = 1
local line = {}
local eof = true
while eof do
line[i] = file:read()
if line[i] == nil then eof = false end
print (line[i])
i = i + 1
end
else
print ("File doesn't exist.")
end
--]]
elseif input == "1" then
-- Code for Inputting data
---[[ Input data code
file = fs.open(filename, "a")
while eof do
write("Input data : ")
local data = read()
if data == nil then eof = false end
file.write(data.."\n") -- where /n is your seperator code
end
file.close()
--]] end input data code
else
print("Command not recognised...")
sleep(2)
end
end
7 posts
Posted 02 July 2013 - 07:37 AM
I was playing around with the ideas a little and wrote this. I hope it can help you with your problems and hopefully allow you to save the data to a file
without too much trouble.
[
Spoiler
local filename = "datalist" -- set you filename to datalist
local tableTop = { "" } -- Make sure the table is clear and defined
local cmpid = os.getComputerID() -- Part of all my programs - comp ID for networking
-- function section
-- Main Program section
while true do
-- now we are looping
term.clear()
term.setCursorPos(1,1)
Print("computer ID - " .. cmpid) -- part of all my programs Id the comp
Print()
print("Please choose an option")
print(" 1. Input Data")
print(" 2. List Data")
print(" 3. Shutdown")
print()
write(" : ")
local input = read()
if input == "3" then
os.shutdown()
elseif input == "2" then
-- code for listing data
---[[ Filename - opening tutorial
-- this commented code can be disabled by one - char.
if fs.exists(filename) == true then
file = io.open(filename, "r")
local i = 1
local line = {}
local eof = true
while eof do
line[i] = file:read()
if line[i] == nil then eof = false end
print (line[i])
i = i + 1
end
else
print ("File doesn't exist.")
end
--]]
elseif input == "1" then
-- Code for Inputting data
---[[ Input data code
file = fs.open(filename, "a")
while eof do
write("Input data : ")
local data = read()
if data == nil then eof = false end
file.write(data.."\n") -- where /n is your seperator code
end
file.close()
--]] end input data code
else
print("Command not recognised...")
sleep(2)
end
end
Hello , thanx for the help! :)/> ! it was really useful , thanx! :)/>