Quote:
Originally Posted by ownedbycats
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".