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

Convert a string to a variable?

Started by gknova61, 11 November 2012 - 06:56 PM
gknova61 #1
Posted 11 November 2012 - 07:56 PM
Here is my example:


l1 = "village"
l2 = "farm"

aInput = read()
aInput = "l"..aInput
  rednet.send(locationID1,"portal_"..aInput.."_open")
  rednet.send(homePortal,"portal_home_open")
  sleep(3.5)
  rednet.send(locationID1,"portal_"..aInput.."_close")
  rednet.send(homePortal,"portal_home_close")
I'd like to make this so if someone were to type in "1" as aInput, that it'd concentate the letter "l" to the beginning of aInput, making for a final variable string of "l1" which would translate to the variable being "village" then it would send but that's not happening here. Here's the basic jist of what i want:

aInput = 1
l
l
v
aInput = l1
l
l
v
aInput = village
l
l
v
rednet sends "portal_village_open"
Lyqyd #2
Posted 11 November 2012 - 08:01 PM
Not sure the names are still going to be what you want, but you're basically looking for a table lookup:


l = {"village", "farm"}

aInput = tonumber(read())
if l[aInput] then
    rednet.send(locationID1, "portal_"..l[aInput].."_open")
    rednet.send(homePortal,"portal_home_open")
    sleep(3.5)
    rednet.send(locationID1,"portal_"..l[aInput].."_close")
    rednet.send(homePortal,"portal_home_close")
end

So, when you type 1, it uses the first index in the table "l", which contains "village".
gknova61 #3
Posted 11 November 2012 - 08:06 PM
Perfect Lyqyd, thanks!