This would be a good idea, but multiple chest/slot combos need to be saved, because only ever having one stack of an item seems useless.
It's more the use of the technique to avoid iteration that I'm getting at. To expand on it, consider this block around 412 for eg:
Spoiler
for key,value in pairs(entries) do --Insert the new stack into the list
if key > 2 then
if tonumber(string.sub(value,1,string.find(value,",")-1)) == chest then
if tonumber(string.sub(value,string.find(value,",")+1,string.find(value,":")-1)) == slot then
break
elseif tonumber(string.sub(value,string.find(value,",")+1,string.find(value,":")-1)) < slot then
table.insert(entries, key, chest..","..slot..":"..amount)
break
end
elseif tonumber(string.sub(value,1,string.find(value,",")-1)) < chest then
table.insert(entries, key, chest..","..slot..":"..amount)
break
end
if key == table.getn(entries) then
table.insert(entries, chest..","..slot..":"..amount)
end
end
if key % 20 == 0 then
sleep(0)
end
end
Now let's say that instead of having keys along the lines of 2,3,4,5,6,etc with strings attached to them as values, we used the
strings as the keys (and assigned whatever value we like just for the sake of having them, eg "true"):
Spoiler
entries[chest..","..slot..":"..amount] = true
… and it's in the table. If for whatever reason you want to iterate through that table with a "for key,value in pairs(entries) do" loop, then you simply pay attention to "key" instead of "value" as you go. Wanna remove such entries? Just set 'em to nil.
You'll notice this loses your neat ordering, but I'm not sure why that's needed. Again, I'm skimming. In any case this skips all those slow table.insert()/remove()s that often work by, you guessed it, iterating (putting aside the loops you yourself are putting those calls in).
Another option: don't save strings in your "entries" table, save tables:
Spoiler
for key,value in pairs(entries) do --Insert the new stack into the list
if key > 2 then
if value.chest == chest then
if value.slot == slot then
break
elseif value.slot < slot then
table.insert(entries, key, {["chest"] = chest, ["slot"] = slot, ["amount"] = amount})
break
end
elseif value.slot < chest then
table.insert(entries, key, {["chest"] = chest, ["slot"] = slot, ["amount"] = amount})
break
end
if key == table.getn(entries) then
table.insert(entries, {["chest"] = chest, ["slot"] = slot, ["amount"] = amount})
end
end
if key % 20 == 0 then
sleep(0)
end
end
Consider the difference in the amount of string searches and text conversions as your tables fill up with stuff. Heck, you could could combine both methods and use tables like these as your keys, if you were so inclined:
Spoiler
entries[{["chest"] = chest, ["slot"] = slot, ["amount"] = amount}] = true
I'm just nit-picking now, but "if key % 20 == 0 then" is also a bit slow (and only gets slower as "key" gets higher). Something like "if bit.band(key,31)==0 then" should be a fair bit faster - very loosely put, if "key" can be evenly divided by 32, it'll resolve as true, but no actual division is being processed.
I'm tempted to go on about ways to avoid table.insert()/table.remove() if you "must" stick with numeric indexing, but it's late and I'm already rambling.