How to make an app - Andromo App Maker

Announcement

Collapse
No announcement yet.

Remove a table item when its Index is unknown

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • BioHazard
    started a topic Remove a table item when its Index is unknown

    Remove a table item when its Index is unknown

    Will just leave this one up here for anyone interested.

    While trying to figure out how to remove a table item when its Index number is dynamic (ie. unknown), I stumbled across a custom function to employ with Table.Remove which makes this possible.

    Custom function: TableFindIndex.
    Example useage attached.
    Adapted from: here
    Attached Files

  • BioHazard
    replied
    Or,

    ...we can go even simpler, by just employing the value-matching method originally expressed in the custom function above. Like this:
    Code:
    tData = {
        "Japan",
        "Russia",
        "USA"
    };
    
    tValue = "Russia";
    
    Debug.Clear();
    Debug.ShowWindow(true);
    
    for i, v in pairs (tData) do    
        if v == tValue then        
            Table.Remove(tData, i)            
            for i, v in pairs (tData) do
                Debug.Print(v .. "\r\n");    
            end
        end
    end

    Probably should've just posted this one first - now that I've managed to confuse everybody!
    This board needs an EDIT button. Hint, hint!

    Leave a comment:


  • BioHazard
    replied
    Or,

    ...it can also be done without the custom function, by using the String.Find command in a for/do loop. Like this:
    Code:
    tData = {
        "Japan",
        "Russia",  -- here is our table of data
        "USA"
    };
    
    tValue = "Russia";  -- here is our table Value (whose Index number we assume is unknown)
    
    Debug.Clear();
    Debug.ShowWindow(true);
    
    for i, v in pairs (tData) do -- loop through table (tData)    
        if String.Find(v, tValue, 1, false) >0 then -- if "Russia" (tValue) is found, returns number >0 
                                                    -- and catches tValue's Index number (stored in 'i').        
            Table.Remove(tData, i) -- now we can use Table.Remove command in normal way                
            
            for i, v in pairs (tData) do -- loop through newly modified table
                Debug.Print(v .. "\r\n"); -- and print remaining Values        
            end
        end
    end

    Leave a comment:


  • BioHazard
    replied
    re-post,

    Sorry, some superfluos naming conventions in above example. Is functionally correct - but inconsistent variable naming may cause confusion.

    Function code should read:
    Code:
    function TableFindIndex (tData, tValue)
        for index, value in pairs(tData) do
            if value == tValue then
                return index
            end
        end
    end
    Example useage code has been adjusted accordingly. Corrected APZ now re-upped and attached below.

    LOL, an edit button on this board would be a nice thing wouldn't it?
    Attached Files

    Leave a comment:

Working...
X