Jump to content




Is it possible to do this?

lua

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

#1 Micheal Pearce

  • Members
  • 87 posts
  • LocationUSA

Posted 14 April 2013 - 08: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]])


#2 Imque

  • Members
  • 134 posts

Posted 14 April 2013 - 08: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"




#3 Micheal Pearce

  • Members
  • 87 posts
  • LocationUSA

Posted 14 April 2013 - 08:44 PM

Thanks Man/Girl/Demon Baby

#4 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 14 April 2013 - 08:48 PM

View PostYuri, on 14 April 2013 - 08:44 PM, said:

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


#5 diegodan1893

  • Members
  • 164 posts
  • LocationSpain

Posted 15 April 2013 - 06:54 AM

Doing tostring(var) when printing isn't necessary, it will do it automatically for you.

#6 JokerRH

  • Members
  • 147 posts

Posted 15 April 2013 - 09:35 AM

View Postdiegodan1893, on 15 April 2013 - 06:54 AM, said:

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)

#7 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 21 April 2013 - 12:17 AM

View PostJokerRH, on 15 April 2013 - 09:35 AM, said:

View Postdiegodan1893, on 15 April 2013 - 06:54 AM, said:

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])


#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 April 2013 - 12:31 AM

View PostLordIkol, on 21 April 2013 - 12:17 AM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users