remove elements in a listHello,
someone can help me.....i have two lists like this:
MYLIST = {:empty, :empty2, :Name("A:k"), :Name("A:m"), :Name("A:x"), :Name("A:y"), :NA, :Name("D:3"), :Name("D:6"), :Name("D:8")};
DELCOLS= {:empty, :empty2};
i would like remove the elements of list DELCOLS from MYLIST
expected results:
MYLIST = {:Name("A:k"), :Name("A:m"), :Name("A:x"), :Name("A:y"), :NA, :Name("D:3"), :Name("D:6"), :Name("D:8")}
thanks in advance
Gianpaolo
Re: remove elements in a listJeff's code is great and use a clever way to remove any multiple instances of the unwanted items.
Just to illustrate that there's often more than one way to do things in JSL: here's an alternative approach using Associative Arrays.
(Note, all multiples would be removed)
MYLIST = {:empty, :empty2, :Name("A:k"), :Name("A:m"), :Name("A:x"), :Name("A:y"), :NA, :Name("D:3"), :Name("D:6"), :Name("D:8")};
DELCOLS = {:empty, :empty2};
// Remove DELCOLS from MYLIST. Retains original order of items.
AA = Associative Array(MYLIST, 1 :: N Items(MYLIST));
MYLIST = (AA << Remove(Associative Array(DELCOLS)) << get keys)[Rank(AA << get values)];
// If order is not important, this simpler version works (items sorted alphabetically)
MYLIST_sorted = Associative Array(MYLIST) << Remove(Associative Array(DELCOLS)) << get keys;
Show(MYLIST, MYLIST_sorted);
Re: remove elements in a list@txnelson is right, the Scripting Index and Scripting Guide are excellent resources.
To help you along the way though, check out the For() loop, Remove From(), As List() and Loc() functions.
MYLIST = {:empty, :empty2, :Name( "A:k" ), :Name( "A:m" ), :Name( "A:x" ), :Name( "A:y" ), :NA,
:Name( "D:3" ), :Name( "D:6" ), :Name( "D:8" )};
DELCOLS = {:empty, :empty2};
For( i = 1, i <= N Items( delcols ), i++,
Remove From( mylist, As List( Loc( mylist, delcols[i] ) ) )
);
Re: remove elements in a listYou would use the Remove() function. See the scripting index
Help==>Scripting Index
for details.
I strongly suggest you read the Scripting Guide. It contains a detail on how to manipulate lists
Help==>Books==>Scripting Guide