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

variable variables name?!

Started by ZKlack, 03 June 2015 - 08:36 AM
ZKlack #1
Posted 03 June 2015 - 10: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
Bomb Bloke #2
Posted 03 June 2015 - 10: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.
Creator #3
Posted 03 June 2015 - 03:05 PM
If you want it to be in the "root" table, you can use _G["something"] = "blablabla"
flaghacker #4
Posted 03 June 2015 - 03:09 PM
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 on 03 June 2015 - 01:10 PM
Creator #5
Posted 03 June 2015 - 03:23 PM
Well, but he wants to have variable variable names, and that is a possible way. Else using local tables is smarter.
flaghacker #6
Posted 03 June 2015 - 04:12 PM
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.
Creator #7
Posted 03 June 2015 - 04:19 PM
Ok. I thought I needed some too, before I learned tables.
Edited on 03 June 2015 - 02:19 PM
ZKlack #8
Posted 04 June 2015 - 12:10 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.
.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
cmdpwnd #9
Posted 05 June 2015 - 07: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 :)/>