View Single Post
Old 04-29-2021, 06:02 AM   #85
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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Code:
program:
	g = field('#genres');
	k = field('#kobocoll');

	if list_contains(g, ',', '^Fanfiction$', '1', '') then
		k = list_union(k, 'Fanfiction', ',')
	fi;

	if list_contains(g, ',', '^Cozy Mystery$', '1', '') then
		k = list_union(k, 'Cozy Mysteries', ',')
	fi;

	if list_contains(g, ',', '^(Fantasy|Science Fiction)$', '1', '') then
		k = list_union(k, 'Fantasy & Sci-Fi', ',')
	fi;

	k
How would I run the first match it finds? For example, if I have a book with "Cozy Mystery" and "Fantasy" in g, I only want it to add Cozy Mysteries.

Use 'elif' (else if) instead of 'if'. Assuming all three are mutually exclusive and in priority order, then this:
Code:
program:
	g = field('#genres');
	k = field('#kobocoll');

	if list_contains(g, ',', '^Fanfiction$', '1', '') then
		k = list_union(k, 'Fanfiction', ',')
	elif list_contains(g, ',', '^Cozy Mystery$', '1', '') then
		k = list_union(k, 'Cozy Mysteries', ',')
	elif list_contains(g, ',', '^(Fantasy|Science Fiction)$', '1', '') then
		k = list_union(k, 'Fantasy & Sci-Fi', ',')
	fi;

	k
EDIT: If the sequence of choices is long then the first_non_empty() function may be more appropriate than a sequence of ifs. As in:
Code:
program:
	g = field('#genres');
	k = field('#kobocoll');

	v = first_non_empty(
		list_contains(g, ',', '^Fanfiction$', 'Fanfiction', ''),
		list_contains(g, ',', '^Cozy Mystery$', 'Cozy Mysteries', ''),
		list_contains(g, ',', '^(Fantasy|Science Fiction)$', 'Fantasy & Sci-Fi', '')
	);

	list_union(k, v, ',')

Last edited by chaley; 04-29-2021 at 06:08 AM.
chaley is offline   Reply With Quote