Quote:I'm not sure what is going through your head here:
pots = { tea = {3}, ballad = {3}, jade = {1}, tears = {2} }
I tried to explain that in one of my posts but i will try to clarify what i meant. I am trying to use table.insert to track how many potions are available and display the number available in the listbox
Quote:
I am using the nested tables to make editing the values easier since i can't figure out how to make it edit the value instead of the table itself without them. My first tries caused a new key and value to be added to pots instead of changing them.
using modified version of your code
if result then
print ("You chose", choices [result])
qty = tonumber (string.match (choices [result], "%((%d+)%)"))
print ("Quantity =", qty )
table.insert (pots, tonumber (string.match (choices [result], "%((%d+)%)")) -1)
else
print "Nothing chosen"
end -- if
end
now if we call the script 2 times and choose ballad which had a value of 3 at the start, the value hasn't changed and displays the same in the listbox as 'ballad (3)' instead of 'ballad (1)' using '/tprint (pots)' shows that two new keys have been added 1=2 and 2=2
Quote:
And cannot use strings for the pos in table.insert (t, pos, v)
if result then
print ("You chose", choices [result])
qty = tonumber (string.match (choices [result], "%((%d+)%)"))
print ("Quantity =", qty )
table.insert (pots, (choices[result]), tonumber (string.match (choices [result], "%((%d+)%)")) -1)
else
print "Nothing chosen"
end -- if
returns this error when you run the script
[string "Script file"]:26: bad argument #2 to 'insert' (number expected, got string)
stack traceback:
[C]: in function 'insert'
[string "Script file"]:26: in function <[string "Script file"]:9>
I was using the nested tables with the numbered key to allow me to use 1 as the 'pos' value in table.insert
I just cannot figure out how to make it edit the value of the key being selected instead of the table itself by adding keys using your method,or to edit the correct sub-table without making a long conditional using the one I was trying.
|