1. Post #1
    joyenusi's Avatar
    May 2006
    148 Posts
    Say I have a list presented as a string, such as the following;

    local items = eggs,milk,bananas
    

    And I want to remove bananas from the string, but the string could also appear as;
    local items1 eggs,bananas,milk
    local items2 bananas,eggs,milk
    

    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)

  2. Post #2
    I had to buy my own title because no one loves me
    BL00DB4TH's Avatar
    April 2011
    239 Posts
    string.gsub
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 1 (list)

  3. Post #3
    joyenusi's Avatar
    May 2006
    148 Posts
    Wouldn't that remove all instances of banana though ?

    I wasn't clear enough in my example.

    Consider the following string;
    local items = "egg,milk,banana,banana"
    


    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;
    local items = "banana,egg,milk,banana"
    

    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".

  4. Post #4
    Gold Member
    Donkie's Avatar
    July 2009
    1,280 Posts
    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, ",")
    
    Reply With Quote Edit / Delete Reply Windows Vista Sweden Show Events Agree Agree x 1 (list)

  5. Post #5
    joyenusi's Avatar
    May 2006
    148 Posts
    -snip- (nevermind)

    Cheers.