View Single Post
Old 07-11-2018, 12:10 PM   #4
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
Ok, time to get into some details.

1) Lets first fix your non-working example.
Code:
program:
 t = field('tags');
 strcat
 ( 
		contains(t, "tag1", 'letter-a-icon.png:', ''), 
                contains(t, "tag3", 'letter-b-icon.png:', ''),
                contains(t, "tag4", 'letter-b-icon.png:', ''),
		contains(t, "tag5", 'letter-d-icon.png:', ''), 
		contains(t, "tag6", 'letter-x-icon.png:', ''), 
		contains(t, "tag7", 'letter-i-icon.png:', ''), 
		contains(t, "tag9", 'letter-p-icon.png:', ''), 
		contains(t, "tag10", 'letter-p-icon.png:', ''), 
		contains(t, "tag11", 'letter-r-icon.png:', ''), 
		contains(t, "tag12", 'letter-s-icon.png:', ''), 
		contains(t, "tag13", 'letter-s-icon.png:', ''), 
		contains(t, "tag14", 'letter-x-icon.png:', ''), 
		contains(t, "tag15", 'letter-w-icon.png:', ''),
		test(or(
				contains(t, "tag2", 'y', ''),
				contains(t, "tag8", 'y', ''),
			), 'letter-c-icon.png:', '')
 )
The problem with your example is that the value of the "or" was thrown away, making the strcat always add letter-c-icon.png to the result. Using "test" takes care of that problem, adding letter-c-icon.png only if the "or" returns a non-empty value.

2) Efficient "or": the "contains" function actually searches using a regular expression, not a basic string. This means that using the "or" capability built into regexps ('val1|val2') you can do "or" tests without changing very much. For example, the following replaces your first example, with the added benefit of better performance. I didn't bother to copy the tag1 through tag12 lines.

Code:
program:
 t = field('tags');
 strcat
 ( 
		contains(t, "tag13", 'letter-s-icon.png:', ''), 
		contains(t, "tag14", 'letter-x-icon.png:', ''), 
		contains(t, "tag15", 'letter-w-icon.png:', ''),
		contains(t, "tag2|tag8", 'letter-c-icon.png:', '')
 )
3) There is a gotcha: substrings in tags. Consider an example where you want an icon for fidelity instead of infidelity. This template would do it:
Code:
program:
 t = field('tags');
 strcat
 ( 
		contains(t, "fidelity|monogamy", 'letter-m-icon.png:', '')
 )
Unfortunately it would also match the tag "infidelity" because it contains the substring "fidelity". There is an easy way to avoid this as long as your tags contain only alphanumeric or underscores: use the regexp boundary test \b which matches if the previous/next character is not alphanumeric or underscore. For example:
Code:
program:
 t = field('tags');
 strcat
 ( 
		contains(t, "\bfidelity\b|monogamy", 'letter-m-icon.png:', '')
 )
would not match "infidelity" because the 'n' in front of 'fidelity' does not satisfy the boundary test.
chaley is offline   Reply With Quote