|
| 20 May 2013 02:55 AM |
How can I get this to print a as val1/2/3 instead of printing the integer?
val1 = 1 val2 = 2 val3 = 3
a = math.max(val1,val2,val3) print(a) |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 20 May 2013 03:02 AM |
val1 = 1 val2 = 2 val3 = 3
a = math.max(val1,val2,val3) print("val"..a) |
|
|
| Report Abuse |
|
|
|
| 20 May 2013 03:05 AM |
Well what I mean is if it is possible to change an integer back to its variable state after doing this, for example...
cheese = 1 bananas = 2 choc = 3
because they are not necessary have integers at the end of the variable... |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 20 May 2013 03:15 AM |
You could do this
function GetVarName(value) for num,obj in pairs(getfenv(0)) do if obj == value then return num end end
val1 = 1 val2 = 2 val3 = 3
a =GetVarName( math.max(val1,val2,val3))
print(a)
This might not work as expected, if you'll have variables with same value. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
| |
|
|
| 20 May 2013 03:23 AM |
Thank you it works, I'm reasonably new to scripting so sorry for asking you to make me something instead of help :)
Thanks again! |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 20 May 2013 03:30 AM |
For more accurate results, you can do:
t = {} t.var1 = 1 t.var2 = 2 t.var3 = 3
function GetVarName(value,t) for num,obj in pairs(t) do if obj == value then return num end end end
print(GetVarName(math.random(3),t))
Still it will get first equal value it finds in table.
|
|
|
| Report Abuse |
|
|