This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

pass a variable to his value

Started by FuuuAInfiniteLoop(F.A.I.L), 16 January 2013 - 12:52 AM
FuuuAInfiniteLoop(F.A.I.L) #1
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
remiX #2
Posted 16 January 2013 - 01:55 AM
What's it doing?

try

function setVal(key, value)
   data[key] = value
end
FuuuAInfiniteLoop(F.A.I.L) #3
Posted 16 January 2013 - 01:59 AM
thanks why i dont mid on that xDDD
theoriginalbit #4
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 [ ]
ChunLing #5
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.