View Single Post
Old 12-21-2020, 07:08 AM   #7
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,448
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I think you want a new item list consisting of
  • The first level of certain hierarchical items
  • All other items, split at a period
  • Combined together to remove duplicates.
  • Then sorted
If so, then this template does it.
Code:
program:
	g = field('#genre');
	stripped_hierarchies = list_re_group(g, ',', '.',
			'^(Fanfiction|Documentation and Manuals)($|\..*$)',
			'{$}', '');
	split_hierarchies = re(stripped_hierarchies, '\.', ',');
	cleaned = list_union('', split_hierarchies, ',');
	sorted = list_sort(cleaned, 0, ',')
The order of arguments to list_union is important. The function checks for duplicates while adding the second list to the first. It doesn't remove existing duplicates from the first. By passing an empty list as the first list, duplicates in the second list are removed as they are added to the first.

EDIT: added list_sort to the template.

Last edited by chaley; 12-21-2020 at 12:26 PM. Reason: Edit: added list_sort
chaley is offline   Reply With Quote