View Single Post
Old 02-26-2011, 10:42 AM   #5
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,475
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Doug-W View Post
One problem though is that it doesn't help when the docs are incorrect For example the largest problem I had was that the docs say:

# test(text if not empty, text if empty) – return text if not empty if the field is not empty, otherwise return text if empty.

Not test(expression_to_test, text if not empty, text if empty) which is how it is apparently used. I thought it took the expression to the left of the test, but now that I understand a bit better I see how the left expression fits in particularly with 'program'
The documentation is correct for the section it is in, single function mode. In that mode, the 'value' is always the field named in the beginning of the template and is never supplied as an argument, no argument is quoted, and spaces are significant. In template program mode or general program mode, the 'value' (first) argument must be provided and all constant arguments must be quoted. In template program mode, the variable '$' is assigned the value of the field used in the template. For example, for {series:'test($, 'A', 'B')'} the $ will be assigned the value contained in the field 'series'.
Quote:
Final question just to be sure, does test take a regex of which a field value just happens to be a static test fed to the regex engine? If so that's kind of neat, I've been using regular expressions for 20 years, and never thought to use '.' for contains data, and '$' for always true.
I don't understand the question.

First, let's assume that we are using general program mode so we don't need to worry about which quote to use.

The 'test' function checks if value is or is not empty. There is no regexp involved. Examples:
Code:
test('', 'A', 'B') will return 'B'
test('something', 'A', 'B') will return 'A'
For completeness: in single-function mode, the template
Code:
{series:test(A,B)}
will return 'A' if the series field is not empty. It is exactly equivalent to the template program mode expression
Code:
{series:'test($, "A", "B")'}
If you want to test using a regexp then the switch function is for you. Examples:
Code:
switch('ABC', '.', 'A', 'B') will return 'A'
switch('ABC', '^$', 'A', 'B') will return 'B'
switch('ABC', 'AB', 'A', 'B') will return 'A'
switch('ABC', '^.*$', 'A', 'B') will return 'A'
chaley is offline   Reply With Quote