How do you create a new key-value pair using variables as substitutes for key and value elements?

Suppose I have a table: k = { key1 = 10, key2 = 20, key3 = 30 }. I want to append a new key to the already existing table such that the new key-value pair would be, { key4 = 40 }. 

Now let’s say that I have a function that auto-generates the key value pair, returns the key and value separately and assigns them in the variables, lock and pw. So now, lock would be key4, and pw would be 40. 

If I create a new key-value pair in the form of { lock = pw }, why does it literally become { lock = 40 }, and not { key4 = 40 } ? What would be the fix for this?

hey dan

you would need to do 

k[lock] = pw

hey dan

you would need to do 

k[lock] = pw