- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
How to check if a list contains any values from another list?
Is there an elegant one-liner for that rather than iterate through the second list using Contains() function?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to check if a list contains any values from another list?
You can loop through one of the lists and use Contains() to see if the specific list item is found anywhere in the other list.
Or, you can convert both of the lists into separate Associative Arrays and then check for the Intersect between the two arrays.
Names Default To Here( 1 );
listA = {"a", "b", "c"};
listB = {"d", "c", "f"};
inBothList = {};
For Each( {member}, listA,
If( Contains( listB, member ),
Insert Into( inBothList, member )
)
);
Show( inBothList );
arrayA = Associative Array( listA );
arrayB = Associative Array( listB );
arrayA << intersect( ArrayB);
show( arrayA );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to check if a list contains any values from another list?
Not one-liner, but more elegant than a loop:
A = {1, 2, 3, 4, 5};
B = {3, 6, 7};
check = Associative Array(A);
check << Intersect(Associative Array(B));
N Items(check);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to check if a list contains any values from another list?
You can loop through one of the lists and use Contains() to see if the specific list item is found anywhere in the other list.
Or, you can convert both of the lists into separate Associative Arrays and then check for the Intersect between the two arrays.
Names Default To Here( 1 );
listA = {"a", "b", "c"};
listB = {"d", "c", "f"};
inBothList = {};
For Each( {member}, listA,
If( Contains( listB, member ),
Insert Into( inBothList, member )
)
);
Show( inBothList );
arrayA = Associative Array( listA );
arrayB = Associative Array( listB );
arrayA << intersect( ArrayB);
show( arrayA );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to check if a list contains any values from another list?
I usually use either For Each + Break or Associative Arrays but there are many different methods where you can use functions a bit "incorrectly" such as Transform Each ("one-liner" such as this are usually horrible ideas (in my opinion) as they tend to be difficult to read)
Names Default To Here( 1 );
A = {1, 2, 3, 4, 5};
B = {3, 6, 7};
r = Eval(Transform Each({item}, A, Output("Expression", "Any"), Contains(B, item)));
It also depends on what you are comparing: strings, integers, floats, lists, ... as JMP lists can basically contain anything.