Combining lists

Here are a few snippets of Magic Data that can be used to combine lists in Uber List or anywhere else that evaluates Magic Data expressions. 

There are primarily two types of combinations between lists, the superset of both lists and the intersection between lists.

Combination or Union of lists

For the combination of lists, we can merge the lists in various ways. While these examples are listing files in file sets, the same principles can be used for any list such as pages, users, products or whatever you like.

Accumulating in a Memory

Our first solution uses a memory to accumulate the resulting list.

SET "first set name" AS_FILESET LIST_FILES 500 MERGE_INTO "my_list"
SET "second set name" AS_FILESET LIST_FILES 500 MERGE_INTO "my_list"

The symbol MERGE_INTO appends a list to a memory and returns the content of the memory with duplicates removed. So at the end of the line 2, the memory "my_list" contains the combined list and that list is immediately available for any following symbol. This can be extended with as many lists as you like.

SET "first set name" AS_FILESET LIST_FILES 500 MERGE_INTO "my_list"
SET "second set name" AS_FILESET LIST_FILES 500 MERGE_INTO "my_list"
SET "second third name" AS_FILESET LIST_FILES 500 MERGE_INTO "my_list" 

Directly merging lists

An alternative way of merging lists is to use subexpressions and merge the results directly

SET "first set name" AS_FILESET LIST_FILES 500
MERGE
( SET "second set name" AS_FILESET LIST_FILES 500 )

Here the key symbol is MERGE and the parenthesis after denotes a subexpression that returns another list. The result is exactly the same and again we can extend the pattern to combine many lists.

SET "first set name" AS_FILESET LIST_FILES 500
MERGE
( SET "second set name" AS_FILESET LIST_FILES 500 )
MERGE
( SET "third set name" AS_FILESET LIST_FILES 500 )

Intersection of lists

This second pattern becomes more useful when we want the intersection of two or more lists, i.e. A list of all items that appear in both lists, but not those that are only in one of the lists. Here there is no easy accumulation into a memory and we need to work directly with the lists.

SET "first set name" AS_FILESET LIST_FILES 500
INTERSECT
( SET "second set name" AS_FILESET LIST_FILES 500 )

The only change is to use the symbol INTERSECT in place of MERGE. Again, it can be extended to multiple lists.

SET "first set name" AS_FILESET LIST_FILES 500
INTERSECT
( SET "second set name" AS_FILESET LIST_FILES 500 )
INTERSECT
( SET "third set name" AS_FILESET LIST_FILES 500 )

Exclusion of lists

Another useful variation of this pattern is to list items that are in the first list, but not in a second list. Again it uses a similar pattern, but using the symbol REMOVE_FROM_LIST.

SET "first set name" AS_FILESET LIST_FILES 500
REMOVE_FROM_LIST
( SET "second set name" AS_FILESET LIST_FILES 500 )

As before, the pattern can be extended to exclude multiple lists from the first list.

SET "first set name" AS_FILESET LIST_FILES 500
REMOVE_FROM_LIST
( SET "second set name" AS_FILESET LIST_FILES 500 )
REMOVE_FROM_LIST
( SET "third set name" AS_FILESET LIST_FILES 500 )

Using the lists

The above list patterns can be used directly as the list expression in an Uber List block. In other circumstances, perhaps you want to turn a list of identities into something more manageable such as a list of names and output that list.

SET "Screengrabs" AS_FILESET LIST_FILES 100
/* Insert whatever combining or other manipulation of lists as per above */
APPLY_EACH
  FILE_NAME
END_APPLY_EACH
SORT_BY
  /* No need for any content here because each item is now a file name */
END_SORT_BY
HTML_OL

Here we use APPLY_EACH to replace each item in the list with its file name, then SORT_BY to then sort the list by item.

Other Lists

As noted at the beginning, while these examples are listing files in file sets, the same principles can be used for any list such as pages, users, products or whatever you like.

For example, suppose we were writing some Magic Data on the concrete5.org site and wanted to list PRB members who were not also PRB Administrators.

SET "PRB Members" AS_GROUP LIST_USERS 50
REMOVE_FROM_LIST
( SET "PRB Administrators" AS_GROUP LIST_USERS 50 )

We could use this in an Uber List of users, or with some regular Magic Data follow that with additional expressions to provide email links

SORT_BY
  USERNAME
END_SORT_BY
APPLY_EACH
  EMAIL
  AS_NAMED_LINK ( THAT USERNAME )
END_APPLY_EACH
HTML_UL

A point worth noting is the use of THAT USERNAME. THIS returns the entry value to the current subexpression, which in this context is what comes into the immediate parenthesis. We need to go one level of subexpression further back to get what is provided by APPLY_EACH, hence we use THAT.

Last updated: over a year ago