• Removing items from a string
    4 replies, posted
  • Avatar of joyenusi
  • Say I have a list presented as a string, such as the following; [lua] local items = eggs,milk,bananas [/lua] And I want to remove bananas from the string, but the string could also appear as; [lua] local items1 eggs,bananas,milk local items2 bananas,eggs,milk [/lua] What would be a good way of removing an item from the string list whilst maintaining the structure of the list (i.e. item1,item2,item3)
  • Avatar of joyenusi
  • Wouldn't that remove all instances of banana though ? I wasn't clear enough in my example. Consider the following string; [lua] local items = "egg,milk,banana,banana" [/lua] I want to remove one banana from the list whilst maintaining the sequence "item1,item2" whilst also accounting for banana being at the beginning of the list, as seen in the following; [lua] local items = "banana,egg,milk,banana" [/lua] So from my understanding, using string.gsub would remove all instances of banana in the first string but in the second string wouldn't remove at all because it would be looking for ",banana".
  • Avatar of Donkie
  • [lua]local itemstbl = string.Explode(",", items) for key, item in ipairs(itemstbl) do if item == "banana" then table.remove(itemstbl, key) break end end items = table.concat(itemstbl, ",") [/lua]