Difference between revisions of "Conditional Functions"

From JRiverWiki
Jump to: navigation, search
(FirstNotEmpty(…))
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{See Also|Expression Functions}}
+
{{Function Type Page}}
__FORCETOC__
+
 
 
The functions in this section test one or more arguments to produce either a true or false outcome, and execute specific actions depending upon that result.
 
The functions in this section test one or more arguments to produce either a true or false outcome, and execute specific actions depending upon that result.
  
Line 6: Line 6:
 
However, these can be easily emulated using any of several techniques. See: [[Boolean Operations]].
 
However, these can be easily emulated using any of several techniques. See: [[Boolean Operations]].
  
The NOT operator <span style="font-family: monospace,monospace; font-size:1em;"><b>!</b></span> (exclamation point) may be used in a conditional to invert the sense of the conditional test.  Inverting the sense of a test can make reading expressions easier, or support better [[#IfElse|IfElse()]] sequences.
+
The NOT operator {{monospace|<b>!</b>}} (exclamation point) may be used in a conditional to invert the sense of the conditional test.  Inverting the sense of a test can make reading expressions easier, or support better [[#IfElse|IfElse()]] sequences.
  
=== If(&hellip;) ===
+
=== <span id="If">If(&hellip;)</span> ===
Conditional if-else evaluator.
+
: Conditional if-else evaluator.
  
{| style="width: 100%; border-spacing: 0; border: 0px solid black;" align="top" cellpadding="3" cellspacing="0"
+
{{function description box
|- id="If" valign="top"
+
| name=If
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 2px 1px 0 2px; border-right: 1px solid #bbb;" width="100" | Description
+
| arguments=test expression, true expression, false expression
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>if(</b><i>test expression</i><b>, </b><i>true expression</i><b>, </b><i>false expression</i><b>)</b></span>
+
| description=
The [[#If|If()]] function is used to evaluate a <i>test expression</i>, and will output the result of the <i>true expression</i> or <i>false expression</i>, depending upon the evaluation result.  The <i>test expression</i> is expected to return a 0 (false value) or a non-zero (true value).
+
The [[#If|If()]] function is used to evaluate a <i>test expression</i>, and will output the result of the <i>true expression</i> or <i>false expression</i>, depending upon the evaluation result.  The <i>test expression</i> is expected to return a 0 (false value) or a non-zero (true value). Nesting is allowed. If the <i>test expression</i> is preceded by the NOT operator (!, an exclamation point), the sense of the test is inverted.  Non-zero values are inverted to 0, and 0 is inverted to 1.
Nesting is allowed.
+
| examples=
If the <i>test expression</i> is preceded by the NOT operator (!, an exclamation point), the sense of the test is inverted.  Non-zero values are inverted to 0, and 0 is inverted to 1.
+
'''{{monospace|if(isequal([artist], bob dylan, 1), Genius, Mediocre)}}'''
|- valign="top"
+
: Outputs {{monospace|Genius}} when artist is (case insensitive) Bob Dylan and {{monospace|Mediocre}} otherwise.
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples
+
'''{{monospace|if(isequal([artist], bob dylan, 1), Genius, if(isequal([album], Joshua Tree, 8), Great Album, Mediocre))}}'''
|style="background: #f9f9f9; color: #111; border-style: solid; border-width: 0px 2px 2px 0; border-top: 1px solid #bbb;" | <span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>if(isequal([artist], bob dylan, 1), Genius, Mediocre)</nowiki></b></span>
+
: This nested [[#If|If()]] expression expands on the previous example, by first evaluating if the artist is Bob Dylan, and outputs {{monospace|Genius}} if true. When the artist is not Bob Dylan, the album is then tested to see if it is {{monospace|Joshua Tree}}, and if so outputs {{monospace|Great Album}}, otherwise outputs {{monospace|Mediocre}}.
<p style="margin-left:20pt;">Outputs <span style="font-family: monospace,monospace; font-size:1em;">Genius</span> when artist is (case insensitive) Bob Dylan and <span style="font-family: monospace,monospace; font-size:1em;">Mediocre</span> otherwise.</p>
+
'''{{monospace|if(!isempty([comment]), regex([comment], /#^(\\S+\\s+\\S+\\s+\\S+)#/, 1), *No Comment)}}'''
<span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>if(isequal([artist], bob dylan, 1), Genius, if(isequal([album], Joshua Tree, 8), Great Album, Mediocre))</nowiki></b></span>
+
: Outputs the first three words of the comment field; otherwise, outputs *No Comment.  By using the NOT operator, the sense of the conditional is inverted so that the more interesting case is moved ahead of the more mundane case.
<p style="margin-left:20pt;">This nested [[#If|If()]] expression expands on the previous example, by first evaluating if the artist is Bob Dylan, and outputs <span style="font-family: monospace,monospace; font-size:1em;">Genius</span> if true.
+
}}
When the artist is not Bob Dylan, the album is then tested to see if it is <span style="font-family: monospace,monospace; font-size:1em;">Joshua Tree</span>, and if so outputs <span style="font-family: monospace,monospace; font-size:1em;">Great Album</span>, otherwise outputs <span style="font-family: monospace,monospace; font-size:1em;">Mediocre</span>.</p>
 
<span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>if(!isempty([comment]), regex([comment], /#^(\\S+\\s+\\S+\\s+\\S+)#/, 1), *No Comment)</nowiki></b></span>
 
<p style="margin-left:20pt;">Output's the first three words of the comment field; otherwise, outputs *No Comment.  By using the NOT operator, the sense of the conditional is inverted so that the more interesting case is moved ahead of the more mundane case.</p>
 
|}
 
<div style="text-align:right;">([[#top|Back to top)]]</div>
 
  
=== IfElse(&hellip;) ===
+
=== <span id="IfElse">IfElse(&hellip;)</span> ===
Conditional if-elseif evaluator
+
: Conditional if-elseif evaluator.
  
{| style="width: 100%; border-spacing: 0; border: 0px solid black;" align="top" cellpadding="3" cellspacing="0"
+
{{function description box
|- id="IfElse" valign="top"
+
| name=IfElse
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 2px 1px 0 2px; border-right: 1px solid #bbb;" width="100" | Description
+
| arguments=test1, action1, test2, action2, test3, action3, &hellip;
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>ifelse(</b><i>test1</i><b>, </b><i>action1</i><b>, </b><i>test2</i><b>, </b><i>action2</i><b>, </b><i>test3</i><b>, </b><i>action3</i><b>, </b><i>&hellip;</i><b>)</b></span>
+
| description=
The [[#IfElse|IfElse()]] conditional provides a convenient mechanism for shortening and more clearly expressing nested conditionals into an alternating sequence of tests and actions.
+
The [[#IfElse|IfElse()]] conditional provides a convenient mechanism for shortening and more clearly expressing nested conditionals into an alternating sequence of tests and actions. One or more test/action pairs may be specified.
One or more test/action pairs may be specified.
 
  
 
For example, consider a nested sequence of [[#If|If()]] tests such as the following pseudo-code:
 
For example, consider a nested sequence of [[#If|If()]] tests such as the following pseudo-code:
<div style="font-family: monospace,monospace; font-size:1em;">
+
<code>
<div style="margin-left: 20pt">if (<i>test1</i>)</div>
+
:: if (test1)
<div style="margin-left: 40pt"><i>action1</i></div>
+
::: action1
<div style="margin-left: 20pt">else if (<i>test2</i>)</div>
+
:: else if (test2)
<div style="margin-left: 40pt"><i>action2</i></div>
+
::: action2
<div style="margin-left: 20pt">else if (<i>test3</i>)</div>
+
:: else if (test3)
<div style="margin-left: 40pt"><i>action3</i></div>
+
::: action3
</div>
+
</code>
  
 
The [[#IfElse|IfElse()]] statement may be used to more cleanly express the flow of expression by removing the superfluous internal [[#If|If()]] statements, converting the clumsy expression:
 
The [[#IfElse|IfElse()]] statement may be used to more cleanly express the flow of expression by removing the superfluous internal [[#If|If()]] statements, converting the clumsy expression:
  
<div style="margin-left: 20pt"><span style="font-family: monospace,monospace; font-size:1em;">if(<i>test1</i>, <i>action1</i>, if(<i>test2</i>, <i>action2</i>, if(<i>test3</i>, <i>action3</i>)))</span></div>
+
: {{monospace|if(<i>test1</i>, <i>action1</i>, if(<i>test2</i>, <i>action2</i>, if(<i>test3</i>, <i>action3</i>)))}}
  
 
into the more elegant:
 
into the more elegant:
  
<div style="margin-left: 20pt"><span style="font-family: monospace,monospace; font-size:1em;">ifelse(<i>test1</i>, <i>action1</i>, <i>test2</i>, <i>action2</i>, <i>test3</i>, <i>action3</i>)</span></div>
+
: {{monospace|ifelse(<i>test1</i>, <i>action1</i>, <i>test2</i>, <i>action2</i>, <i>test3</i>, <i>action3</i>)}}
  
 
If any of the test expressions <i>test1</i>, etc. are preceded by the NOT operator (!, an exclamation point), the sense of that test is inverted.  Non-zero values are inverted to 0, and 0 is inverted to 1.
 
If any of the test expressions <i>test1</i>, etc. are preceded by the NOT operator (!, an exclamation point), the sense of that test is inverted.  Non-zero values are inverted to 0, and 0 is inverted to 1.
|- valign="top"
+
| examples=
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples
+
'''{{monospace|ifelse(isequal([media type], Audio), Le Tunes, isequal([media type], Video), Flix)}}'''
|style="background: #f9f9f9; color: #111; border-style: solid; border-width: 0px 2px 2px 0; border-top: 1px solid #bbb;" | <span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>ifelse(isequal([media type], Audio), Le Tunes, isequal([media type], Video), Flix)</nowiki></b></span>
+
: If media type is audio, outputs {{monospace|Le Tunes}}, else if media type is video, outputs {{monospace|Flix}}.
<p style="margin-left:20pt;">If media type is audio, outputs <span style="font-family: monospace,monospace; font-size:1em;">Le Tunes</span>, else if media type is video, outputs <span style="font-family: monospace,monospace; font-size:1em;">Flix</span></p>
+
'''{{monospace|ifelse(isequal([artist], Bob Dylan), Genius, isequal([album], Joshua Tree, 8), Great Album, 1, Mediocre)}}'''
<span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>ifelse(isequal([artist], Bob Dylan), Genius, isequal([album], Joshua Tree, 8), Great Album, 1, Mediocre)</nowiki></b></span>
+
: This example, implements the nested if statements from the If() section above, first testing if the artist is Bob Dylan, and if true, outputs {{monospace|Genius}}, otherwise evaluates the second test to determine if the album is {{monospace|Joshua Tree}}, and if true, outputs {{monospace|Great Album}}, otherwise, performs a final test, in this case a degenerate test of 1 (and 1 is always true), thus outputting the value {{monospace|Mediocre}}.
<p style="margin-left:20pt;">This example, implements the nested if statements from the If() section above,
+
}}
first testing if the artist is Bob Dylan, and if true, outputs <span style="font-family: monospace,monospace; font-size:1em;">Genius</span>,
 
otherwise evaluates the second test to determine if the album is <span style="font-family: monospace,monospace; font-size:1em;">Joshua Tree</span>,
 
and if true, outputs <span style="font-family: monospace,monospace; font-size:1em;">Great Album</span>, otherwise, performs a final test,
 
in this case a degenerate test of 1 (and 1 is always true), thus outputting the value <span style="font-family: monospace,monospace; font-size:1em;">Mediocre</span>.</p>
 
|}
 
<div style="text-align:right;">([[#top|Back to top)]]</div>
 
  
=== FirstNotEmpty(&hellip;) ===
+
=== <span id="FirstNotEmpty">FirstNotEmpty(&hellip;)</span> ===
Returns the first non-empty argument.
+
: Returns the first non-empty argument.
  
{| style="width: 100%; border-spacing: 0; border: 0px solid black;" align="top" cellpadding="3" cellspacing="0"
+
{{function description box
|- id="FirstNotEmpty" valign="top"
+
| name=FirstNotEmpty
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 2px 1px 0 2px; border-right: 1px solid #bbb;" width="100" | Description
+
| arguments=value1, value2, &hellip;
| style="background: #f9f9f9; color: #111; border-style: solid; border-width: 2px 2px 0 0" width="1200" | <span style="font-family: monospace,monospace; font-size:1em; color:#0f3f8d; font-size:110%"><b>firstnotempty(</b><i>value1</i><b>, </b><i>value2</i><b>, </b><i>&hellip;</i><b>)</b></span>
+
| description=
 
The [[#FirstNotEmpty|FirstNotEmpty()]] function acts as a conditional by returning the first argument from <i>value1</i>, <i>value2</i>, ... that is not empty.
 
The [[#FirstNotEmpty|FirstNotEmpty()]] function acts as a conditional by returning the first argument from <i>value1</i>, <i>value2</i>, ... that is not empty.
Two or more arguments may be used, and the first non-empty argument is returned.
+
Two or more arguments may be used, and the first non-empty argument is returned. With two arguments, is is functionally equivalent to the sequence such as {{monospace|if(!isempty(<i>value1</i>), <i>value1</i>, <i>value2</i>)}}. With more than two arguments, [[#FirstNotEmpty|FirstNotEmpty()]] avoids long nested [[#If|If()]] sequences that simply test for emptiness.
With two arguments, is is functionally equivalent to the sequence such as <span style="font-family: monospace,monospace; font-size:1em;">if(!isempty(<i>value1</i>), <i>value1</i>, <i>value2</i>)</span>.
+
| examples=
With more than two arguments, [[#FirstNotEmpty|FirstNotEmpty()]] avoids long nested [[#If|If()]] sequences that simply test for emptiness.
+
'''{{monospace|firstnotempty([media sub type], Misc Video)}}'''
|- valign="top"
+
: Returns the value in {{monospace|media sub type}} if it is not empty, otherwise returns {{monospace|Music Video}}.
! scope="row" style="background: #ecedf3; color: #111; border-style: solid; border-width: 0px 1px 2px 2px; border-top: 1px solid #bbb; border-right: 1px solid #bbb;" | Examples
+
'''{{monospace|firstnotempty([series], [name], Tag your Videos!)}}'''
|style="background: #f9f9f9; color: #111; border-style: solid; border-width: 0px 2px 2px 0; border-top: 1px solid #bbb;" | <span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>firstnotempty([media sub type], Misc Video)</nowiki></b></span>
+
: Returns the first non-empty value from the fields {{monospace|series}} or {{monospace|name}}, and if both are empty, returns the reminder to {{monospace|Tag your Videos!}}.
<p style="margin-left:20pt;">Returns the value in <span style="font-family: monospace,monospace; font-size:1em;">media sub type</span> if it is not empty, otherwise returns <span style="font-family: monospace,monospace; font-size:1em;">Music Video</span>.</p>
+
}}
<span style="font-family: monospace,monospace; font-size:1em;"><b><nowiki>firstnotempty([series], [name], Tag your Videos!)</nowiki></b></span>
 
<p style="margin-left:20pt;">Returns the first non-empty value from the fields <span style="font-family: monospace,monospace; font-size:1em;">series</span> or <span style="font-family: monospace,monospace; font-size:1em;">name</span>, and if both are empty, returns the reminder to
 
<span style="font-family: monospace,monospace; font-size:1em;">Tag your Videos!</span>.</p>
 
|}
 
<div style="text-align:right;">([[#top|Back to top)]]</div>
 

Revision as of 15:59, 25 January 2016

The functions in this section test one or more arguments to produce either a true or false outcome, and execute specific actions depending upon that result.

The expression language does not directly support AND, OR, and XOR operations. However, these can be easily emulated using any of several techniques. See: Boolean Operations.

The NOT operator ! (exclamation point) may be used in a conditional to invert the sense of the conditional test. Inverting the sense of a test can make reading expressions easier, or support better IfElse() sequences.

If(…)

Conditional if-else evaluator.
Description If(test expression, true expression, false expression)

The If() function is used to evaluate a test expression, and will output the result of the true expression or false expression, depending upon the evaluation result. The test expression is expected to return a 0 (false value) or a non-zero (true value). Nesting is allowed. If the test expression is preceded by the NOT operator (!, an exclamation point), the sense of the test is inverted. Non-zero values are inverted to 0, and 0 is inverted to 1.

Examples if(isequal([artist], bob dylan, 1), Genius, Mediocre)
Outputs Genius when artist is (case insensitive) Bob Dylan and Mediocre otherwise.

if(isequal([artist], bob dylan, 1), Genius, if(isequal([album], Joshua Tree, 8), Great Album, Mediocre))

This nested If() expression expands on the previous example, by first evaluating if the artist is Bob Dylan, and outputs Genius if true. When the artist is not Bob Dylan, the album is then tested to see if it is Joshua Tree, and if so outputs Great Album, otherwise outputs Mediocre.

if(!isempty([comment]), regex([comment], /#^(\\S+\\s+\\S+\\s+\\S+)#/, 1), *No Comment)

Outputs the first three words of the comment field; otherwise, outputs *No Comment. By using the NOT operator, the sense of the conditional is inverted so that the more interesting case is moved ahead of the more mundane case.

IfElse(…)

Conditional if-elseif evaluator.
Description IfElse(test1, action1, test2, action2, test3, action3, …)

The IfElse() conditional provides a convenient mechanism for shortening and more clearly expressing nested conditionals into an alternating sequence of tests and actions. One or more test/action pairs may be specified.

For example, consider a nested sequence of If() tests such as the following pseudo-code:

if (test1)
action1
else if (test2)
action2
else if (test3)
action3

The IfElse() statement may be used to more cleanly express the flow of expression by removing the superfluous internal If() statements, converting the clumsy expression:

if(test1, action1, if(test2, action2, if(test3, action3)))

into the more elegant:

ifelse(test1, action1, test2, action2, test3, action3)

If any of the test expressions test1, etc. are preceded by the NOT operator (!, an exclamation point), the sense of that test is inverted. Non-zero values are inverted to 0, and 0 is inverted to 1.

Examples ifelse(isequal([media type], Audio), Le Tunes, isequal([media type], Video), Flix)
If media type is audio, outputs Le Tunes, else if media type is video, outputs Flix.

ifelse(isequal([artist], Bob Dylan), Genius, isequal([album], Joshua Tree, 8), Great Album, 1, Mediocre)

This example, implements the nested if statements from the If() section above, first testing if the artist is Bob Dylan, and if true, outputs Genius, otherwise evaluates the second test to determine if the album is Joshua Tree, and if true, outputs Great Album, otherwise, performs a final test, in this case a degenerate test of 1 (and 1 is always true), thus outputting the value Mediocre.

FirstNotEmpty(…)

Returns the first non-empty argument.
Description FirstNotEmpty(value1, value2, …)

The FirstNotEmpty() function acts as a conditional by returning the first argument from value1, value2, ... that is not empty. Two or more arguments may be used, and the first non-empty argument is returned. With two arguments, is is functionally equivalent to the sequence such as if(!isempty(value1), value1, value2). With more than two arguments, FirstNotEmpty() avoids long nested If() sequences that simply test for emptiness.

Examples firstnotempty([media sub type], Misc Video)
Returns the value in media sub type if it is not empty, otherwise returns Music Video.

firstnotempty([series], [name], Tag your Videos!)

Returns the first non-empty value from the fields series or name, and if both are empty, returns the reminder to Tag your Videos!.