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

Is it possible to do this?

Started by Micheal Pearce, 14 April 2013 - 07:31 PM
Micheal Pearce #1
Posted 14 April 2013 - 09:31 PM
is it possible to doing a code like this and have it print what X is = to?


Test = {
T1 = {
   X = 1
   Y = 1
}
}
print(Test[T1[X]])
Imque #2
Posted 14 April 2013 - 09:33 PM
Yes but a better format is:



test = {
	T1 = {
	  X = 1,
	  Y = 2
	}
}

print(test.T1.X) -- Returns 2



You can even do this:



test = {
  [1] = {
    text = {
	  val1 = "Some"
    }
  }
}

print(test[1].text.val1) -- returns "Some"


Micheal Pearce #3
Posted 14 April 2013 - 09:44 PM
Thanks Man/Girl/Demon Baby
superaxander #4
Posted 14 April 2013 - 09:48 PM
Thanks Man/Girl/Demon Baby
Hmm I think it's a dog

On topic:
If you want to iterate trough the values thus might come in hand

for identifier, value in pairs(table) do
    print(tostring(identifier).." contains:"..tostring(value))
end
diegodan1893 #5
Posted 15 April 2013 - 07:54 AM
Doing tostring(var) when printing isn't necessary, it will do it automatically for you.
JokerRH #6
Posted 15 April 2013 - 10:35 AM
Doing tostring(var) when printing isn't necessary, it will do it automatically for you.

You can only concatenate a string with a string (or a table with __concat metamethod)
LordIkol #7
Posted 21 April 2013 - 01:17 AM
Doing tostring(var) when printing isn't necessary, it will do it automatically for you.

You can only concatenate a string with a string (or a table with __concat metamethod)

thats not true you can concatenate string and number without tostring. see Example:


a = 1
d = "1"
c = {1,"1"}
print("Number: " .. a)
print("String: " .. d)
print("both from a table " ..c[1]..c[2])
theoriginalbit #8
Posted 21 April 2013 - 01:31 AM
thats not true you can concatenate string and number without tostring. see Example:
-snip-
that is because strings and numbers have __concat defined, booleans, tables and some others do not.