Adding multiple values to keys in dictionary using table.unpack

I’ve been trying to associate multiple values to a key in a dictionary, but for some reason the old values are overriden ,

code :

      
   strings = {
	
	["player"] = {"a","b","c"}
	
   }
   
   total = strings["player"]--get an array of values
   
   print( table.unpack(total))--> prints all values ( a b c )
 
   values = table.unpack(total)--so I can reference multiple values
   print(values)-- only prints a !
 
   strings["player"] =  values ,"new string?"
 
  print(#strings.player)--> 2

Apparently when referencing the table.unpack statement, and printing it, the values just disappear and the first is kept. How would you do this ? are there various methods to do so? if so, then how

Any help at all will be appreciated

an image of the output

image

1 Like

Well, how can the variable values be three things at once? When you do table.unpack it returns all the arguments from the given table. To get them all you’d need to do something like

value1, value2, value3 = table.unpack(total);

What exactly are you trying to do? When you use print and table.unpack it’s basically the same as print(value1, value2, value3).

I am aware of what table.unpack does, but here when I reference the table.unpack statement itself, why does printing it only return 1 value rather than when I use print(table.unpack) directly (which prints all of them)

Because print takes a variable number of arguments in a list. That is different than declaring one variable, which isn’t a table. It wouldn’t be possible for it to work because you can’t have one variable hold multiple values as if it was a table.

2 Likes

That explains the printing issue, and it was really helpful , but how would I add more values to the key, (keeping the previous values associated with the key, just inserting new ones with them).

Not exactly sure what you mean, but maybe try this?

local strings = {
    ["player"] = {"a", "b", "c"}
}

table.insert(strings["player"], "new string");

print(strings["player"][4]);
3 Likes