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

How To Make A Table With An Infinite Number Of Variables?

Started by Zenon, 28 June 2015 - 09:23 PM
Zenon #1
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
The_Cat #2
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
Zenon #3
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
InDieTasten #4
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
Zenon #5
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
The_Cat #6
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