Jump to content




Convert a string to a variable?


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

#1 gknova61

  • Members
  • 74 posts

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"

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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".

#3 gknova61

  • Members
  • 74 posts

Posted 11 November 2012 - 08:06 PM

Perfect Lyqyd, thanks!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users