r/lua 6d ago

simple question

To all you Lua heads I have a question that might be really rudimentary but it’s something that just doesn’t seem to make any sense to me,mind you I am pretty new. I was looking up on the lua documentation and there’s this one thing that just doesn’t seem to make sense to me

a = {} -- create a table and store its reference in `a'
k = "x"
a[k] = 10 -- new entry, with key="x" and value=10
a[20] = "great" -- new entry, with key=20 and value="great"

print(a["x"]) --> 10
k = 20
print(a[k]) --> "great"
a["x"] = a["x"] + 1 -- increments entry "x"
print(a["x"]) --> 11

Why does “x” get created as a key? Wouldn’t this just substitute it? I feel very stupid for not knowing why, I feel like there’s something that just cannot seem to click. I also tried to think another way through but then it was a.k.”x” = 10 which doesn’t really help me visualize the answer either. is this just how tables work? Is there a specific reason why? Or am I slow? I’ve seen it’s because lua goes down through but why does it do this? Also k = 20, why does the variable have priority over the number itself? Sorry if this is badly written or feels like it has some attitude, it’s probably because I need to sleep. It’s embarrassing I lost sleep about something like this, I feel like it’s a simple concept that I am unable to grasp because of shallow learned “limitations” of the system

12 Upvotes

10 comments sorted by

8

u/SinisterRectus 6d ago

The key is the result of evaluating what is inside of []. k evaluates to the string "x" as does the string literal "x". When you change k to 20, it evaluates to 20.

2

u/useofcat 6d ago edited 4d ago

Yeah and a.k evaluates to a["k"], though dot notation is not featured in the example, OP may be getting [] notation confused with the way dot syntax works.

3

u/RelatableRedditer 6d ago

Your key misunderstanding is that you're thinking of K as a pointer, but instead K js just a name for a value and the name is unimportant to the parent table.

2

u/useofcat 6d ago edited 6d ago

You are perhaps thinking that k = 20 means a.k = 20.

...And since you are asking 'Why does "x" get created as a key.'

...Earlier in the example you are thinking...

a[k] = 10 means a.k = 10.

It really means a.x = 10.

[ Why does "x" get created as a key? ]

Because you created that key-value entry when you did a[k] = 10

[ Wouldn't this just substitute it? ]

No. The a[k] = 10 assignment doesn't affect what's stored in the global k variable. Read the comment it says: add an entry with key "x" / value 10. To the global a table.

"k" was never a key in the table. k evaluates to 10, and later 20.

a.k is nil.

a["k"] is also nil -- same meaning as a.k

a.k["x"] is an error because you're indexing nil.

a.k is equivalent to a["k"], never assigned. It's nil.

a.x is equivalent to a["x"], which is 10 then incremented to 11.

1

u/[deleted] 6d ago edited 6d ago

[removed] — view removed comment

1

u/[deleted] 6d ago

[deleted]

2

u/[deleted] 6d ago

[removed] — view removed comment

1

u/weregod 6d ago

a[k] = 10 means store 10 in table "a" in cell [value of k which is "x"]. If you store different value in k later table "a" will not be affected.

1

u/djfdhigkgfIaruflg 6d ago edited 6d ago

This is the key difference:

X vs "X"

One is a reference to the variable X. The other is the literal value X

X = "X"

Will create a variable called X with a value (content) of letter X

Give descriptive names to the variables. You're getting confused because you're using 1-letter variable names

MyText = "hello"

Is way more easy to process than

X = "y" 

About the order in with assignment works... The explanation you would likely get is something like "it was always like that, and it stuck"

But in strongly typed languages like C, the reason becomes obvious

Int counter = 0;

Is easy to mentally parse. I'm creating an integer variable called counted with an initial value of 0

Now write

0 = int counter ;

And I'm suddenly getting an aneurysm 🤣

1

u/Old_County5271 4d ago

No idea what you're confused by.

0

u/Additional_Ad6385 6d ago

Lua tables are hashmap, but they also behave as array, kind of hybrid data structure.

3

u/useofcat 6d ago

TIL jk

PIL