View Single Post
Old 01-04-2012, 04:14 PM   #26
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: 11,773
Karma: 7029857
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
It seems that you are saying that:
- A book has zero genre values, or 2 or more genre values.
- If a book has a genre value, the first is either FICTION or NONFICTION, and all subsequent values begin with that value.
- If a book has no genre values, then you want an empty value.
- If a book has genre values, then you want the second value in the sorted list.

If the above is true, then this template gives you the value you want.
Code:
{#genre:'re(cmp(count($, ','), 1, '', $,  sublist(list_sort($, '0', ','), 1, 2, ',')), '\.', '/')'||/}{author}/{title}
The template uses template program mode, which permits nesting of function calls. The outermost function, re, operates on the return value of 'cmp', converting all periods to slashes. The cmp function checks the length of the genre list. If it is less than 1, it returns the empty string. If it is equal to 1 (a case that shouldn't happen according to my understanding of the rules), it returns the single value. If it is longer than 1, it sorts the list and returns the second value.
chaley is offline   Reply With Quote