WebSiphon 2 Guide: SiphonScript Expressions and Operators
contents prev next

Expressions and Operators

Expressions in SiphonScript behave very similar to those found in other language environments. SiphonScript expressions follow an algebraic order of evaluation, and you may enclose sections of an expression in parenthesis to denote an explicit evaluation order. Expressions may be used anywhere a value is specified, including within a list or as a parameter to a function call.

The table below displays all valid operators listed in order of precendence, from lowest to highest. Each of these operators are described in detail further in this chapter.

Operator(s) Description (Group)
& concatenation
? : conditional expressions
and, or, not, &&, ||, ! boolean operators
=, !=, <, <=, >, >=, contains,
starts with, ends with, in
comparison operators
as formatting output
+, -, *, /, %, mod math operators
item x of y, ' list accessors
Math Operators

All basic mathematical computations are performed using this group of operators. If both operands are integers, the result will be an integer. Otherwise, both operands will first be coerced to floating point numbers and the result will be floating point. This coercion is performed to ensure maximum precision during calculation.

Note that both % and mod may be used to perform a modulus operation, which returns the remainder of a division operation.

Operator Description Examples Results
+ addition a = 1 + 3;
b = 1.5 + 0.0001;
c = a + b;
a is now equal to 4
b is now equal to 1.5001
c is now equal to 5.5001
- subtraction a = 1 - 3;
b = 1.5 - 0.0001;
c = a - b;
a is now equal to -2
b is now equal to 1.4999
c is now equal to -3.4999
* multiplication a = 1 * 3;
b = 1.5 * 0.0001;
c = a * b;
a is now equal to 3
b is now equal to 0.00015
c is now equal to 0.00045
/ division a = 1 / 3;
b = 1.5 / 0.0001;
c = a / b;
a is now equal to 0.333333333
b is now equal to 15000.0
c is now equal to 0.000022222
%
mod
modulus a = 34 mod 14;
b = 5 % 2;
c = a % b;
a is now equal to 6
b is now equal to 1
c is now equal to 0
Comparison Operators

Comparison operators are used to perform a test between two values and return a boolean indicating whether or not the operation evaluates true or false.

When a comparison is performed on two values which are not of the same type, an attempt will be made to coerce them prior to evaluation.

Operator Description Examples Results
= equal to 1 = 3
"5" = 5
"a" = "a"
>> false
>> true
>> true
!= not equal to 1 != 3
"5" != 5
"a" != "a"
>> true
>> false
>> false
< less than 1 < 3
"5" < 5
"a" < "b"
>> true
>> false
>> true
<= less than or equal to 1 <= 3
"5" <= 5
"a" <= "b"
>> true
>> true
>> true
> greater than 1 > 3
"5" > 5
"a" > "b"
>> false
>> false
>> false
>= greater than or equal to 1 >= 3
"5" >= 5
"a" >= "b"
>> false
>> true
>> false
contains   "hello!" contains "!"
[1 2 3 "a"] contains 4
>> true
>> false
starts with   "hello!" starts with "!"
[1 2 3 "a"] starts with "1"
>> false
>> true
ends with   "hello!" ends with "!"
[1 2 3 "a"] ends with "a"
>> true
>> true
in   "!" in "hello!"
3 in [1 2 3 "a"]
>> true
>> true
Boolean Operators

Boolean operators are used to build simple or complex expressions within a single statement. Typically, these operators are used to define a condition, as is shown in the examples in the table below.

The and (or &&) and or (or ||) operators are short circuit evaluated. This means that the entire expression will not be evaluated if the result of the boolean operator is already known based on the first operand. For example, if the first operand of an and operation evaluates to false, there is no need to evaluate the second operand. The or operator works the same way if its first operand evaluates to true.

Operator Description Examples Results
not
!
not not(1 = 3)
!("!" in "hello!")
not(["a" "b"] starts with "a")
>> true
>> false
>> false
and
&&
and (1 = 3) and (1 = 1)
("!" in "hello!") && (["a" "b"] starts with "a")
("5" >= 4) and (3 in [1 2 3 "a"])
>> false
>> true
>> true
or
||
or (1 = 3) or (1 = 1)
("a" in "hello!") || (["a" "b"] ends with "a")
("5" >= 4) or (3 in [1 2 3 "a"])
>> true
>> false
>> true
Conditional Expressions

Using the ? and : operators together allow you to form a conditional expression much like an if/then statement. This expression allows you to substitute one value or another depending on a boolean test.

The basic form of a conditional expression is:

boolean-test ? true-value : false-value

If the boolean test evaluates true, true-value will be substituted. Otherwise, false-value will.

Example:

// If the variable "username" has a value, greet the user. Otherwise, 
// ask them to login.
print (username != "") ? "Hello " & username & "!" : "Please login";
Concatenation

Using the & concatenation operator allows you to easily combine multiple values into a single value. The type of the result of the operation is determined by its operands.

A string concatenating with anything but a list will always return a string. If either operand is a list, the resulting value will be a list. If you wish to append a list as a single item in an enclosing list, you must wrap said list within brackets, as is shown in the last two examples below.

Examples:

  1. print "abc" & "def";
    
    >> abcdef
  2. print 12 & 34;
    
    >> 1234
  3. printList([1 2 3] & 45);
    
    >> [1 2 3 45]
  4. printList("mykul" & [4 "claire"]);
    
    >> ["mykul" 4 "claire"]
  5. printList([1 2 3] & [[4 5]]);
    
    >> [1 2 3 [4 5]]
  6. a = [1 2];
    b = [3 4];
    printList([a] & [b]);
    
    >> [[1 2] [3 4]]
Formatting Output

Using the as operator allows you to control how a value is formatted when output. The output could be used in a returned web page, an e-mail being sent, or even a file on disk.

The basic form is:

value as format-specifier

The format-specifier above consists of either a type followed by optional paramters, or a standard C printf() format specifier string. Valid type names using the former method are "integer", "float", or "text". The optional parameters, enclosed in parenthesis like a function call, specify the field size and precision. If you wish to specify on the precision and not the field size you may, but the separating comma must be included.

The examples below demonstrate several ways of formatting output to a web page using the print statement. Each of the examples include a > prior to the output and < after the output to exhibit the formatting of the value, including any spaces added to match the specified field size.

Keep in mind that the as operator does not change the actual datatype of a variable or value. It only formats output. If you need to change the type of a variable, use the coerceTo() function.

Examples:

  1. print ">" & (234.001 as integer) & "<";
    
    >> >234<
  2. print ">" & (234.001 as integer(5)) & "<";
    
    >> >  234<
  3. print ">" & (234.001 as integer(5,5)) & "<";
    
    >> >00234<
  4. print ">" & (64.35892 as float(8,2)) & "<";
    
    >> >   64.36<
  5. print ">" & (64.35892 as float(,3)) & "<";
    
    >> >64.359<
  6. print ">" & ("hello" as text) & "<";
    
    >> >hello<
  7. print ">" & ("hello" as text(10)) & "<";
    
    >> >     hello<
  8. print ">" & ("hello" as text(10,2)) & "<";
    
    >> >        he<
List Accessors

Items within a list can be accessed using several different methods, which are equal and fully equivalent to each other. The method you use is purely a personal style decision. List indexes range from 1 to the total number of items in the list. If an out-of-range index value is used, it will be clipped to the actual list size.

Variables or arbitrarily complex expressions may be used as index values when accessing an item in a list as well, as shown in the last example below.

Examples:

  1. my_list = [1 "hello" [2 "jenna" 3] "four"];
    print item 1 of my_list;
    
    >> 1
  2. print item 2 of item 3 of my_list;
    
    >> jenna
  3. print my_list'2;
    
    >> hello
  4. print my_list'3'1;
    
    >> 2
  5. printList(my_list'3);
    
    >> [2 "jenna" 3]
  6. print itemAt(my_list, 4);
    
    >> four
  7. print "<ol>";
    repeat with x from 1 to sizeOf(my_list)
      print "<li>" & (item x of my_list);
    end repeat;
    print "</ol>";
    
    >> 1. 1
       2. hello
       3. 2 banana 3
       4. four

contents prev next

Copyright (c)1996-2003 Purity Software