1 posts
Posted 15 March 2014 - 03:10 PM
Hi guys! I am new in the forum, first I can some speak English :P/> my from a Turkey, but I am good in the ComputerCraft. Today I want show my Simple String Saver, It's a very basic but efficency (I can write wrong :P/>) code in the down;
stringSaver = { } -- This is table for save strings.
x = 0 -- This is saved string number
for y =1,3 do -- You can change it, this give 3 string for you.
print("Enter String: ") -- A simple Print! :P/>
input = io.read() -- For read input
x = x + 1 -- This add x because if x don't add 1 then x = x and last input is table[1]
table[x] = input -- Table string number equals to input in line
end -- You know it :P/>
while true do -- Try for program all times
print("Match string?: ") -- It's ask match string, if you write 1 then this print table[1]
input = tonumber(io.read()) -- This translate string to number if you dont do then you get nil
print(table[input]) - Print a your input
end -- THE END!
You can use in the Lua, this code don't have got any api, if you like it please comment to topic :P/>
1852 posts
Location
Sweden
Posted 16 March 2014 - 01:23 PM
I can't really see a use for this, And in the for loop you could have done this
for i = 1, 3 do
print( "Enter String: " )
local input = read() -- Made this local to this loop
stringSaver[i] = input
end
Do you see what I did there? I removed the uneccessary( < pretty sure I spelled that wrong :P/> ) variable
x and also made the
variable
input local. Also noticed you put table in the loop, Shouldn't it be stringSaver?
Edited on 16 March 2014 - 12:24 PM
156 posts
Posted 17 March 2014 - 01:52 PM
you could also just drop input all together:
for i = 1, 3 do
print( "Enter String: " )
stringSaver[i] = read()
end
500 posts
Posted 17 March 2014 - 06:37 PM
Heck, if you were adventurous you could even do this:
for i = 1, 3 do
stringSaver[i] = print("Enter String: ") and read()
end
1852 posts
Location
Sweden
Posted 17 March 2014 - 08:09 PM
Yay! I'm gonna be even more adventurous with the full code! :D/>
local stringSaver = {}
for i = 1, 3 do
stringSaver[i] = print("Enter String: ") and read()
end
while true do
print("Table index: ")
print(stringSaver[tonumber(io.read())])
end
It's a pretty cool program for your first released by the way.
Edited on 18 March 2014 - 09:44 AM
1281 posts
Posted 17 March 2014 - 09:16 PM
Yay! I'm gonna be even more adventurous with the full code! :D/>
local stringSaver = {}
for i = 1, 3 do
table[i] = print("Enter String: ") and read()
end
while true do
print("Table index: ")
print(table[tonumber(io.read())])
end
It's a pretty cool program for your first released by the way.
Lol, you stored the values in the table api…
Anyways, continuing the trend!
local stringSaver = {}
for i = 1, 3 do
stringSaver[i] = print("Enter String: ") and read()
end
while true do
print("Table index:\n"..(stringSaver[tonumber(io.read())] or "Invalid index!")
end
7508 posts
Location
Australia
Posted 17 March 2014 - 09:21 PM
-snip-
CometWolf the print in your while wouldn't work, syntax error and order of operations error :P/>
1281 posts
Posted 17 March 2014 - 10:20 PM
blargh forgot a parenthese, i don't see any other error though?
print("Table index:\n"..(stringSaver[tonumber(io.read())] or "Invalid index!"))
7508 posts
Location
Australia
Posted 17 March 2014 - 11:51 PM
blargh forgot a parenthese, i don't see any other error though?
print("Table index:\n"..(stringSaver[tonumber(io.read())] or "Invalid index!"))
Order of operations. I'd do the read, tonumber it, look it up in the table and then print "Table index: [value]" or "Table index: Invalid index!". I assume you wanted it to print "Table index: " before the read, like the other code examples.
Edited on 17 March 2014 - 10:52 PM
1281 posts
Posted 18 March 2014 - 05:26 AM
Now that you mention it, that does make more sense :P/>
1852 posts
Location
Sweden
Posted 18 March 2014 - 10:47 AM
Lol, you stored the values in the table api…
Ooops x) Edited the post :P/>