52 posts
Posted 28 June 2015 - 11:23 PM
Hi All! I Was Wondering How One Would Create A Table That Could Be Used Again And Again, Without Rewriting Variables.
Example:
input = {}
How Would I Make It So That input Will Have An Infinite Number Of Variables, And Not Rewrite Any?
[NOTE]
Im Trying To Use This With Read() Currently, And I Know There Are Alternatives -Im Using One Right Now- But I Dont Want To Define Them Myself
121 posts
Posted 28 June 2015 - 11:27 PM
is this what you mean?
input = {
text="hello", --# String type (you need commas at the end of each time you set a variable)
isMale=true, --# Boolean type
age=20 --# Int type (last one doesn't need a comma :D/>/>/>)
}
print(input.text) --# Writes the text "hello"
print(input.age)
If not, heres a link to the Lua wiki (theres a lot more ways of using tables)
http://lua-users.org.../TablesTutorial
Edited on 28 June 2015 - 09:32 PM
52 posts
Posted 28 June 2015 - 11:53 PM
is this what you mean?
input = {
text="hello", --# String type (you need commas at the end of each time you set a variable)
isMale=true, --# Boolean type
age=20 --# Int type (last one doesn't need a comma :D/>/>/>/>/>)
}
print(input.text) --# Writes the text "hello"
print(input.age)
If not, heres a link to the Lua wiki (theres a lot more ways of using tables)
http://lua-users.org.../TablesTutorial
No, I Mean Something More Along The Lines Of This:
input = {a, b, c, d, e, f, g}
With Each Variable Being What The User Inputs, However I Dont Want To Type It Out, It Needs To Create It On Its Own
Edit: Thx For The Link To The Tutorial, Gonna Read It Now
Actually, Im Thinking Something Like This Might Work?
x = 1
while true do
input = {}
input[x] = read()
x + 1
end
Edited on 28 June 2015 - 09:50 PM
355 posts
Location
Germany
Posted 29 June 2015 - 12:11 AM
x = 1
while true do
input = {}
input[x] = read()
x + 1
end
If you were to define input = {} outside the while loop yes, that would do it ;)/>
Alternatively:
local inputs = {}
while true do
table.insert(inputs,read())
end
Or:
local inputs = setmetatable({},{__index=table})
while true do
inputs:insert(read())
end
Edited on 28 June 2015 - 10:14 PM
52 posts
Posted 29 June 2015 - 12:19 AM
x = 1
while true do
input = {}
input[x] = read()
x + 1
end
If you were to define input = {} outside the while loop yes, that would do it ;)/>
Alternatively:
local inputs = {}
while true do
table.insert(inputs,read())
end
Or:
local inputs = setmetatable({},{__index=table})
while true do
inputs:insert(read())
end
Interesting, Thanks
121 posts
Posted 29 June 2015 - 12:19 AM
You could also make a little function such as:#
local input = {} --# Table to use for later
function example(text, table)
write(text) --# What you want to show to the user
local tempText = read()
table.insert(table, tempText) --# Inserts the read() that the user inputted
end
--# To us the function you'd put the text you want then the table where you want to store the user input
example("Enter you age: ", input)
This would means you could add this function when ever you wanted, you could also use different tables but also the same one.
Edited on 28 June 2015 - 10:21 PM