Indigo Rose Software

Professional Software Development Tools

 
+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2007
    Posts
    233

    Grin Table Problem : I wanna tMain - tSub = tNew !!!

    Hi all AMS users ..
    I have a great problem that it's simple for you.
    Ok, I have a Main table:
    tMain = {"usa", "iraq", "iran", "afghanestan", "uk"}; --numbers of contents isn't specify exactly. here is for example 5 .
    also, I have a Sub table:
    tSub = {"usa", "uk"}; ----numbers of contents is specify exactly.here is for example 2 .
    I wanna tMain - tSub = tNew !!!
    In other word, I wanna elements of tSub delete from into tMain and save in a new table named as "tNew".

    in result:
    tNew = {"iraq", "iran", "afghanestan"}; --it what i looking for...

    please, guide me for accede it.
    I try for it many , but its haven't result
    Thanks...

  2. #2
    Join Date
    Oct 2007
    Location
    Gensokyo
    Posts
    1,324
    This got me seriously frustrated.

    Code:
    function RemoveFromTable(tMainTable, ...) 
    	for i, v in tMainTable do 
    		for j in arg do 
    			if (String.Find(v, arg[j], 1, false) ~= -1) then 
    				Table.Remove(tMainTable, i); 
    			end 
    		end 
    	end	 
    end 
    
    tMain	= { "usa", "iraq", "iran", "afghanestan", "uk"}; 
    
    RemoveFromTable(tMain, "usa", "uk"); 
    
    sTable = Table.Concat(tMain, "\r\n", 1, TABLE_ALL); 
    
    -- Test.
    Dialog.Message( "Contents of tNew:", sTable);
    Phew.

  3. #3
    Join Date
    Oct 2007
    Posts
    233
    Hi Dear Shadow
    Thanks for the reply
    I check it and reply here...

  4. #4
    Join Date
    Oct 2007
    Posts
    233
    Woo...
    Your method are true ,but it has a little bug because:
    only for test i set numbers into table and result Was incorrect
    but in country-name-sample in post nu 2, it's correctly!!!
    function RemoveFromTable(tMainTable, ...)
    for i, v in tMainTable do
    for j in arg do
    if (String.Find(v, arg[j], 1, false) ~= -1) then
    Table.Remove(tMainTable, i);
    end
    end
    end
    end
    tMain = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    Application.Sleep(100);

    RemoveFromTable(tMain, "1", "2", "3", "4", "5");

    sTable = Table.Concat(tMain, "\r\n", 1, TABLE_ALL);

    -- Test.
    Dialog.Message( "Contents of tNew:", sTable);
    Attached Images

  5. #5
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    @ShadowUK

    One trick I've learned when doing this is to walk the main table backwards. Doing it this way, you're not changing the index of elements you still have to check. For instance, if you remove index 5, index 6 then become 5, 7 becomes 6....

    Walking the table backwards avoids that.

    Code:
    for i=Table.Count(tMainTable), 1, -1 do
    BTW, I love the '...' thing-a-ma-bob, where'd you find that?

  6. #6
    Join Date
    Oct 2007
    Location
    Gensokyo
    Posts
    1,324
    Quote Originally Posted by Worm View Post
    @ShadowUK

    One trick I've learned when doing this is to walk the main table backwards. Doing it this way, you're not changing the index of elements you still have to check. For instance, if you remove index 5, index 6 then become 5, 7 becomes 6....

    Walking the table backwards avoids that.

    Code:
    for i=Table.Count(tMainTable), 1, -1 do
    BTW, I love the '...' thing-a-ma-bob, where'd you find that?
    Friend taught me about functions and arguments, infact he taught me Lua. It just means you can use unlimited arguments. and the arguments come out in the table called 'arg'

    Example:
    Code:
    function Unlimargs(...)
        Table.Remove(arg, Table.Count(arg));
        for i,v in arg do Dialog.Message(i, v) end
    end
    
    Unlimargs(1,2,3,4,5,6,7,8,9,10)
    Sometimes it adds one onto the table so I'd always do the table.remove first.

  7. #7
    Join Date
    Dec 2003
    Location
    Location! Location!
    Posts
    6,137
    Quote Originally Posted by ShadowUK View Post
    Friend taught me about functions and arguments, infact he taught me Lua. It just means you can use unlimited arguments. and the arguments come out in the table called 'arg'

    Example:
    Code:
    function Unlimargs(...)
        Table.Remove(arg, Table.Count(arg));
        for i,v in arg do Dialog.Message(i, v) end
    end
    
    Unlimargs(1,2,3,4,5,6,7,8,9,10)
    Sometimes it adds one onto the table so I'd always do the table.remove first.
    Nice one there!
    Intrigued

  8. #8
    Join Date
    May 2006
    Posts
    5,380
    Code:
    function Unlimargs(...)
        Table.Remove(arg, Table.Count(arg));
        for i,v in arg do Dialog.Message(i, v) end
    end
    
    Unlimargs(1,2,3,4,5,6,7,8,9,10)

    thats a neat trick, will come in handy that for sure
    Open your eyes to Narcissism, Don't let her destroy your life!!

  9. #9
    Join Date
    Oct 2007
    Posts
    233
    omg...
    I'm confused,in Upshot : how do i use for sieve a table from some strings and save leftover of strings in new table?
    please, get me a sample for it.
    Thanks man

  10. #10
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728
    Another approach is to go through tMain, and add each item to tNew one at a time. Before you add each item, check to see if it's in tSub, and if it is, don't add it -- just move on to the next item in tMain.
    --[[ Indigo Rose Software Developer ]]

  11. #11
    Join Date
    Oct 2007
    Posts
    233
    Quote Originally Posted by Lorne View Post
    Another approach is to go through tMain, and add each item to tNew one at a time. Before you add each item, check to see if it's in tSub, and if it is, don't add it -- just move on to the next item in tMain.
    Tanks for he reply
    is there a sample for it?
    I'm Beginner user in AMS.

  12. #12
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728
    Figuring out how to do this would be a good exercise for a beginner.
    --[[ Indigo Rose Software Developer ]]

  13. #13
    Join Date
    Oct 2007
    Location
    Gensokyo
    Posts
    1,324
    I've cracked it. I use this same method in my TableEx action plugin which I'll be releasing later on, unluckily it won't be free. Anyway.

    Code:
    function replace(tTable, sPattern, sReplacement, bCaseSensitive)
    	local tReturnTable = tTable
    	for i, v in (tReturnTable) do
    		tReturnTable[i] = String.Replace(v, sPattern, sReplacement, bCaseSensitive);
    	end
    	return tReturnTable;
    end
    
    tMain = {"RizlaUK", "ShadowUK", "Worm", "Intrigued", "rexzooly", "reteset", "longedge"};
    tSub = {"rexzooly", "ShadowUK", "Worm"};
    
    for i, v in tSub do
    	NewTbl = replace(tMain, v, v.." (Replaced, Position on subtable: "..i..")", true);
    end
    
    sTable = Table.Concat(NewTbl, "\r\n", 1, -1)
    Dialog.Message("::[New Table Contents]::", sTable);

  14. #14
    Join Date
    Oct 2007
    Posts
    233
    Very Nice Shadow

  15. #15
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728
    Another approach -- not necessarily better, but different.

    Code:
    -- find by index in an associative table e.g. {fruit=apple,colour=red}
    function AssociativeTableSubtractMatchingIndexes(tSource, tSub)
        local tNew = {};
        
        if tSource == nil then
            return tNew;
        end
        
        if tSub == nil then
            tNew = tSource;
            return tNew;
        end
        
        for iSource, vSource in tSource do 
            if tSub[vSource] == nil then
                tNew[iSource] = vSource;
            end
        end
        return tNew;
    end
    
    -- find by value in an associative table e.g. {fruit=apple,colour=red}
    function AssociativeTableSubtractMatchingValues(tSource, tSub)
        local tNew = {};
        
        if tSource == nil then
            return tNew;
        end
        
        if tSub == nil then
            tNew = tSource;
            return tNew;
        end
    
        for iSource, vSource in tSource do 
            local bFound = false;
            for iSub, vSub in tSub do 
                if(vSub == vSource) then 
                    bFound = true;
                    break;
                end 
            end
            if(not bFound) then
                tNew[iSource] = vSource;
            end
        end
    
        return tNew;
    end
    
    -- find by value in a numeric table e.g. {"apple","red"}
    function NumericTableSubtractMatchingValues(tSource, tSub)
        local tNew = {};
    
        if tSource == nil then
            return tNew;
        end
        
        if tSub == nil then
            tNew = tSource;
            return tNew;
        end
        
        local iNew = 1;
        for iSource, vSource in tSource do 
            local bFound = false;
            for iSub, vSub in tSub do 
                if(vSub == vSource) then 
                    bFound = true;
                    break;
                end 
            end
            if(not bFound) then
                tNew[iNew] = vSource;
                iNew = iNew + 1;
            end
        end
    
        return tNew;
    end
    
    tMain = {"usa", "iraq", "iran", "afghanestan", "uk", "1", "2", "3"};
    tSub = {"usa", "uk", "1", "3"};
    tNew = NumericTableSubtractMatchingValues(tMain,tSub);
    strTable = Table.Concat(tNew, "\r\n", 1, -1);
    Dialog.Message("New Table Contents:", strTable);
    --[[ Indigo Rose Software Developer ]]

Similar Threads

  1. problem with SQLite
    By Jonas DK in forum AutoPlay Media Studio 6.0
    Replies: 2
    Last Post: 01-09-2007, 10:37 AM
  2. problem with table... help pls
    By Jonas DK in forum AutoPlay Media Studio 6.0
    Replies: 1
    Last Post: 12-29-2006, 05:56 PM
  3. Problem with Empty Table Handling
    By madsen in forum AutoPlay Media Studio 5.0
    Replies: 5
    Last Post: 01-19-2005, 05:44 AM
  4. INFO: Tips for Debugging Action Lists in AutoPlay Media Studio 4.0
    By Support in forum AutoPlay Media Studio 4.0 Examples
    Replies: 0
    Last Post: 10-03-2002, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts