Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 Zenon

  • Members
  • 50 posts

Posted 28 June 2015 - 10: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

#2 The_Cat

  • Members
  • 119 posts

Posted 28 June 2015 - 10: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 by The_Cat, 28 June 2015 - 10:32 PM.


#3 Zenon

  • Members
  • 50 posts

Posted 28 June 2015 - 10:53 PM

View PostThe_Cat, on 28 June 2015 - 10:27 PM, said:

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 by theMCcm, 28 June 2015 - 10:50 PM.


#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 28 June 2015 - 11:11 PM

View PosttheMCcm, on 28 June 2015 - 10:53 PM, said:

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 by InDieTasten, 28 June 2015 - 11:14 PM.


#5 Zenon

  • Members
  • 50 posts

Posted 28 June 2015 - 11:19 PM

View PostInDieTasten, on 28 June 2015 - 11:11 PM, said:

View PosttheMCcm, on 28 June 2015 - 10:53 PM, said:

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

#6 The_Cat

  • Members
  • 119 posts

Posted 28 June 2015 - 11:19 PM

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 by The_Cat, 28 June 2015 - 11:21 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users