Lua: What is the difference in these variable assignments?

FatBoi1942

what is the difference between the variable assignment

    local newpos = {}
newpos.x = 1       ----- or --------- newpos[x] = 1

i know not what i speak but to me these seem to be the same thing if not similar?

Etan Reisner

newpos.x = 1 is the same as newpos["x"] = 1 that is they both set the value stored at key string "x" to 1.

newpos[x] = 1 is different. This is set the value stored at key contents of variable x to 1.

Try it and see.

local newpos = {}

newpos.x = 1
print(newpos.x, newpos["x"], x, newpos[x])

newpos["x"] = 2
print(newpos.x, newpos["x"], x, newpos[x])

local x = "var"
print(newpos.x, newpos["x"], x, newpos[x])

newpos[x] = 3
print(newpos.x, newpos["x"], x, newpos[x])

Results for the above:

1   1   nil nil
2   2   nil nil
2   2   var nil
2   2   var 3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the difference between the GNU Makefile variable assignments =, ?=, := and +=?

From Dev

What is the difference between these two assignments?

From Dev

What is the difference between the assignments to pointers in C?

From Dev

Local variable assignments in Lua scripting using eval command

From Dev

What is the scope of a Lua variable?

From Dev

What is the scope of a Lua variable?

From Dev

What is the difference between = and => for a variable?

From Java

What is the difference of pairs() vs. ipairs() in Lua?

From Dev

What is the difference between #QNAN and #IND in Lua

From Dev

Lua: What's the difference between null and nil?

From Dev

What is the difference between #QNAN and #IND in Lua

From Dev

Variable Assignments in Stylus

From Dev

Matlab multiple variable assignments

From Dev

multiple variable definition and assignments

From Dev

Class parameter variable assignments

From Dev

Shakespeare Variable Assignments Not Working

From Dev

Matlab multiple variable assignments

From Dev

Bash variable expansion and assignments

From Dev

What is the difference in typescript variable exports

From Dev

What's the difference between "$(variable)" and "$(VARIABLE)"

From Dev

What's the difference between "$(variable)" and "$(VARIABLE)"

From Dev

What is the difference between variable === constant & constant === variable

From Dev

In Lua, what is the difference between functions that use ":" and functions that do not?

From Dev

What is the difference between Lua registry with light userdata and references?

From Dev

Using conditions in variable assignments in Python

From Dev

For loop assignments overflow into another variable

From Dev

For loop assignments overflow into another variable

From Dev

Spaces in variable assignments in shell scripts

From Dev

Let block in elisp with no variable assignments

Related Related

  1. 1

    What is the difference between the GNU Makefile variable assignments =, ?=, := and +=?

  2. 2

    What is the difference between these two assignments?

  3. 3

    What is the difference between the assignments to pointers in C?

  4. 4

    Local variable assignments in Lua scripting using eval command

  5. 5

    What is the scope of a Lua variable?

  6. 6

    What is the scope of a Lua variable?

  7. 7

    What is the difference between = and => for a variable?

  8. 8

    What is the difference of pairs() vs. ipairs() in Lua?

  9. 9

    What is the difference between #QNAN and #IND in Lua

  10. 10

    Lua: What's the difference between null and nil?

  11. 11

    What is the difference between #QNAN and #IND in Lua

  12. 12

    Variable Assignments in Stylus

  13. 13

    Matlab multiple variable assignments

  14. 14

    multiple variable definition and assignments

  15. 15

    Class parameter variable assignments

  16. 16

    Shakespeare Variable Assignments Not Working

  17. 17

    Matlab multiple variable assignments

  18. 18

    Bash variable expansion and assignments

  19. 19

    What is the difference in typescript variable exports

  20. 20

    What's the difference between "$(variable)" and "$(VARIABLE)"

  21. 21

    What's the difference between "$(variable)" and "$(VARIABLE)"

  22. 22

    What is the difference between variable === constant & constant === variable

  23. 23

    In Lua, what is the difference between functions that use ":" and functions that do not?

  24. 24

    What is the difference between Lua registry with light userdata and references?

  25. 25

    Using conditions in variable assignments in Python

  26. 26

    For loop assignments overflow into another variable

  27. 27

    For loop assignments overflow into another variable

  28. 28

    Spaces in variable assignments in shell scripts

  29. 29

    Let block in elisp with no variable assignments

HotTag

Archive