Jump to content




variable variables name?!

help

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

#1 ZKlack

  • Members
  • 9 posts
  • LocationRiyadh

Posted 03 June 2015 - 09:36 AM

can I make the variable name variable?
because it will be useful but i don't know how
this is an Example:
function getID()
  write("Floor #" .. count .. "computer ID: ")
  input=read()
  floorID=(input+0)
  return floorID
end
write("Floors: ")
input=read()
floors=(input+0)
count=0
if not count==floors then
  count=count+1
  F1=getID()
end
if not count==floors then
  count=count+1
  F2=getID()
end
if not count==floors then
  count=count+1
  F3=getID()
end
if not count==floors then
  count=count+1
  F4=getID()
end
if not count==floors then
  count=count+1
  F5=getID()
end
if not count==floors then
  count=count+1
  F6=getID()
end
if not count==floors then
  count=count+1
  F7=getID()
end
if not count==floors then
  count=count+1
  F8=getID()
end
if not count==floors then
  count=count+1
  F9=getID()
end
if not count==floors then
  count=count+1
  F10=getID()
end
if not count==floors then
  count=count+1
  F11=getID()
end
if not count==floors then
  count=count+1
  F12=getID()
end
if not count==floors then
  count=count+1
  F13=getID()
end
if not count==floors then
  count=count+1
  F14=getID()
end
if not count==floors then
  count=count+1
  F15=getID()
end
this is very long and it is a waste of time
but if the variable name was a string it will be:
function getID()
  write("Floor #" .. count .. "computer ID: ")
  input=read()
  floorID=(input+0)
  return floorID
end
write("Floors: ")
input=read()
floors=(input+0)
count=0
for i=1,15 do
  if not count==floors then
	count=count+1
	("F" .. i)=getID()
  end
end
very short, isn't it?
but the variable name is not a string so my question is
Is there any way to make the variable name variable?
I hope my question was understandable

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 03 June 2015 - 09:45 AM

Yes, using tables - that's why I suggested them for your Tic Tac Toe game.

In a table, you can store variables against "keys". The keys can be pretty much anything you like; a string, a number, whatever.

Eg:

local myTable = {}

myTable["F1"] = "moo"
print(myTable["F1"])

local i = 2
myTable["F"..i] = "blah"

print(myTable["F2"])

Read this guide for more info and examples.

It's "if count ~= floors then", by the way - "if not count==floors then" does something quite different.

#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 03 June 2015 - 02:05 PM

If you want it to be in the "root" table, you can use _G["something"] = "blablabla"

#4 flaghacker

  • Members
  • 655 posts

Posted 03 June 2015 - 02:09 PM

View PostCreator, on 03 June 2015 - 02:05 PM, said:

If you want it to be in the "root" table, you can use _G["something"] = "blablabla"

But that's really bad practice, your variables should be local wherever possible.

Edited by flaghacker, 03 June 2015 - 02:10 PM.


#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 03 June 2015 - 02:23 PM

Well, but he wants to have variable variable names, and that is a possible way. Else using local tables is smarter.

#6 flaghacker

  • Members
  • 655 posts

Posted 03 June 2015 - 03:12 PM

View PostCreator, on 03 June 2015 - 02:23 PM, said:

Well, but he wants to have variable variable names, and that is a possible way. Else using local tables is smarter.

People often think they need variable variable names, at least I did, because they don't know about tables yet.

You answer was perfectly valid but I wanted to point out to the OP that it's not a good idea.

#7 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 03 June 2015 - 03:19 PM

Ok. I thought I needed some too, before I learned tables.

Edited by Creator, 03 June 2015 - 03:19 PM.


#8 ZKlack

  • Members
  • 9 posts
  • LocationRiyadh

Posted 03 June 2015 - 11:10 PM

View PostBomb Bloke, on 03 June 2015 - 09:45 AM, said:

Yes, using tables - that's why I suggested them for your Tic Tac Toe game.

In a table, you can store variables against "keys". The keys can be pretty much anything you like; a string, a number, whatever.

Eg:

local myTable = {}

myTable["F1"] = "moo"
print(myTable["F1"])

local i = 2
myTable["F"..i] = "blah"

print(myTable["F2"])

Read this guide for more info and examples.

It's "if count ~= floors then", by the way - "if not count==floors then" does something quite different.
.thank you for the big help
.I didn't know any thing about tables
.that will become a useful
.but I'm learning so the next project will take more time
.it is an elevator that controlled by monitors
.it may be hard to setup
.but I'll try to make it as easy as I can

#9 cmdpwnd

  • Members
  • 128 posts
  • LocationHere

Posted 05 June 2015 - 06:59 PM

Or if your just stupid and store everything in a central table and your constantly overwriting previous indexes then you may want variable name vars under your table but like I said that's only if you don't know how a table works or your doing some crazy magic lol :)





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users