View Single Post
Old 10-21-2024, 09:33 AM   #733
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 1ily View Post
Hello! I'm using a template that uses the word count column to assign a book's tags.

I have tags for Comics, Novellas, Novels, and Long. Ideally, anything under 2k words will be a Comic, under 40k is a Novella, 150k is a Novel, and anything above that is Long.

It turns out that books with 100k and higher have the "Novel"/"Long" and the "comic" tags, books with 50k-100k words have the correct tags. Books less than 1k are not being given the "comic" tag and books over 188k are not being given any tags at all.

What am I doing wrong?


Example:

Code:
program:	

words = $$#words;
pages = $$#pages;

new_colls = strcat(
if (words < 1500000 && words > 150000) then 'Long,' fi,
if (words < 150000 && words > 50000) then 'Novel,' fi,
if (words < 40000 && words > 2000) then 'Novella,' fi,
if (words < 2000 && words > 0 && pages > 0) then 'Comic,' fi
)

new_colls
You are using the string (lexical) comparison operator (<) instead of the numeric comparison operator (<#).

As said in the template language manual, which is your friend:
Quote:
Relational operators

Relational operators return '1' if the comparison is true, otherwise the empty string (‘’).

There are two forms of relational operators: string comparisons and numeric comparisons.

String comparisons do case-insensitive string comparison using lexical order. The supported string comparison operators are ==, !=, <, <=, >, >=, in, and inlist. For the in operator, the result of the left hand expression is interpreted as a regular expression pattern. The in operator is True if the value of left-hand regular expression matches the value of the right hand expression. The inlist operator is true if the left hand regular expression matches any one of the items in the right hand list where the items in the list are separated by commas. The matches are case-insensitive.

The numeric comparison operators are ==#, !=#, <#, <=#, >#, >=#. The left and right expressions must evaluate to numeric values with two exceptions: both the string value “None” (undefined field) and the empty string evaluate to the value zero.
BTW: the first_matching_cmp() or the switch_if() functions seem to satisfy your needs and are faster than a series of if statements.

BTW2: it seems you should be using >=# in the second relational clause of the if statements. Otherwise you are skipping exact matches.

Last edited by chaley; 10-21-2024 at 09:37 AM. Reason: Added BTW2
chaley is offline   Reply With Quote