PDA

View Full Version : Table Problem : I wanna tMain - tSub = tNew !!!


Esfahan
04-26-2008, 02:24 AM
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 !!!:huh
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...

ShadowUK
04-26-2008, 03:11 AM
This got me seriously frustrated.


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.

Esfahan
04-26-2008, 05:43 AM
Hi Dear Shadow
Thanks for the reply
I check it and reply here...;)

Esfahan
04-26-2008, 06:26 AM
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);

http://www.indigorose.com/forums/attachment.php?attachmentid=6383&stc=1&d=1209209086

Worm
04-26-2008, 06:40 AM
:yes@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.


for i=Table.Count(tMainTable), 1, -1 do


BTW, I love the '...' thing-a-ma-bob, where'd you find that?

ShadowUK
04-26-2008, 06:46 AM
:yes@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.


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

Intrigued
04-26-2008, 11:09 AM
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:
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!

RizlaUK
04-26-2008, 04:02 PM
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

Esfahan
04-27-2008, 01:02 AM
omg...:huh:huh:huh
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

Lorne
04-29-2008, 11:36 AM
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.

Esfahan
04-29-2008, 12:24 PM
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.;)

Lorne
04-29-2008, 12:52 PM
Figuring out how to do this would be a good exercise for a beginner. :yes

ShadowUK
04-29-2008, 12:57 PM
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.

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);

Esfahan
04-30-2008, 12:55 AM
Very Nice Shadow:yes
:yes:yes:yes:yes:yes:yes:yes:yes:yes

Lorne
04-30-2008, 11:15 AM
Another approach -- not necessarily better, but different.

-- 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);

Esfahan
04-30-2008, 12:17 PM
Admin, Thanks a lot
Cute Code
yes:yes:yes:yes:yes:yes:yes:yes:yes:yes:yes: