Jump to content




pass a variable to his value


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

#1 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

Posted 16 January 2013 - 01:52 AM

function setVal(key, value)
data.key = value
end
this problem is fustrant i wnt to pass the variable to his value and pass that to the table but is not working HEEEELP

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 16 January 2013 - 01:55 AM

What's it doing?

try

function setVal(key, value)
   data[key] = value
end


#3 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

Posted 16 January 2013 - 01:59 AM

thanks why i dont mid on that xDDD

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 January 2013 - 03:30 AM

As stated here under the heading "Tables as Dictionaries", you can access data using the shorthand ( syntactical sugar ) of a . but when assigning or updating a key it must be with [ ]

#5 ChunLing

  • Members
  • 2,027 posts

Posted 16 January 2013 - 05:49 AM

That's a little inaccurate. The exact problem is that data.key is precisely equivalent to data["key"], in which key is the literal string "key" and not whatever is contained in the variable with the identifier "key". Thus you need to use data[key], so that you index by the value contained in the variable with the identifier "key" rather than indexing by the literal string value "key".

You can assign new indexes using the format data.key, but the assignment will always be to the literal string value rather than to any variable associated with the identifier.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users