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

how to print the variable from a users input [Solved]

Started by TR1T0N_, 18 January 2014 - 03:24 PM
TR1T0N_ #1
Posted 18 January 2014 - 04:24 PM
Hello, what i want to do is get a users input using read() then print the variable that may be assigned to that input and returning the value assigned to the variable. However my attempts have so far failed
example code:

var1 = "thisisatest" -- this is the variable i wish to be returned when the users inputs "var1"
input = read()
print(input) -- this is where i want to return the value that is assigned to the variable
any help would be much appreciated.
Edited on 18 January 2014 - 04:27 PM
Imprivate #2
Posted 18 January 2014 - 04:31 PM

var1 = "thisisatest"
input = read()
if input == var1 then -- if their answer has the same content as 'var1' then
  print(var1) -- it will print it
end
I'm not sure if this is what you want to know. If it isn't, please explain in further detail :)/>
Edited on 18 January 2014 - 03:32 PM
wieselkatze #3
Posted 18 January 2014 - 04:33 PM
I think the thing he wanted is "searching" for the variable var1 if the user input is "var1".
So if that variable name the user gave is actually a variable and what its content is.
TR1T0N_ #4
Posted 18 January 2014 - 04:40 PM
yes that is basically what i need i tried using another code that i found on another forum to try to convert the string returned by the players input into a variable so the variable can return what it has been set to. i read somewhere you can use the loadstring() function but i am unsure how to use it
Edited on 18 January 2014 - 03:42 PM
wieselkatze #5
Posted 18 January 2014 - 05:14 PM
For me, this worked:


a = "yourvariable"
input = read()

test = loadstring("return "..input)
setfenv(test, getfenv())
print(test())
TR1T0N_ #6
Posted 18 January 2014 - 05:27 PM
that works perfectly thanks alot for your help.