r/lua 2d 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

13 Upvotes

12 comments sorted by

8

u/SinisterRectus 2d 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 2d ago edited 3m 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 2d 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 2d ago edited 2d 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] 2d ago edited 2d ago

[removed] — view removed comment

1

u/useofcat 2d ago

I don't think scoping or even the dual nature of tables is relevant to OPs question/confusion.

2

u/[deleted] 2d ago

[removed] — view removed comment

1

u/useofcat 2d ago

I think they are just generally confusing themselves with an example that shows how keys can be stored as strings in variables and then used to index tables. But the example doesn't illustrate the syntactic sugar that equates a.k and a["k"] or any usefulness of having keys stored in variables, a more realistic example would be the form a[b[N]] , supposedly with lua it is better to keep things flat in structure though.

1

u/weregod 2d 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 2d ago edited 2d 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 10h ago

No idea what you're confused by.

0

u/Additional_Ad6385 2d ago

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

3

u/useofcat 2d ago

TIL jk

PIL