View Single Post
Old 01-11-2023, 05:51 PM   #476
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,529
Karma: 8075938
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
EDIT: Perhaps as a custom function.
For fun, here is a Python stored template that does what you want. Stored templates like this obviate the need for a lot of seldom-used built-in functions.

The template:
Code:
python:
def evaluate(book, context):
	args = context.arguments
	if args is None or len(args) != 3:
		raise ValueError('first_in_list requires 3 arguments')
	sep = args[1]
	lst = tuple(l.strip() for l in args[0].split(',') if l.strip())
	test_lst = tuple(l.strip() for l in args[2].split(',') if l.strip())
	if not test_lst:
		return ''
	for v in test_lst:
		if v in lst:
			return v
	return ''
The list match isn't a regex and is case sensitive. EDIT: it is trivial to make it case insensitive.

Calling it:
Code:
program:
	first_in_list($tags, ',', 'a, b, Space Opera, c')
When called with a book with tags "Science Fiction, Space Opera, onDevice" it returns "Space Opera".

Last edited by chaley; 01-11-2023 at 06:13 PM.
chaley is offline   Reply With Quote