As any other programming language YCP knows a lot of operators that can be used to act on data.
These are binary operators for comparison of two values. The result is always boolean.
Operator | Datatype | Description |
---|---|---|
== | almost all | True if operands are equal, otherwise false. |
< | almost all | True if left operand is smaller than the right one, otherwise false. |
> | almost all | True if left operand is greater than the right one, otherwise false. |
<= | almost all | True if left operand is smaller or equal to the right one, otherwise false. |
>= | almost all | True if left operand is greater or equal to the right one, otherwise false. |
!= | almost all | True if operands are not equal, otherwise false. |
These are logical operators, that works with boolean datatype, two are binary one is unary. The result is always boolean.
Operator | Datatype | Description |
---|---|---|
&& | boolean | True if both operands are true, otherwise false (logical and). |
|| | boolean | True if at least one of the operands is true, otherwise false (logical or). |
! | boolean | True if the operand if false, otherwise false (logical not). |
These are bit operators that works with integer, two are binary one is unary. The result is always integer.
Operator | Datatype | Description |
---|---|---|
& | integer | Bits of the result number are product of the bits of the operands (bit and). |
| | integer | Bits of the result number are count of the bits of the operands (bit or). |
~ | integer | Bits of the result number are reverted bits of operand (bit not). |
<< | integer | Bits of the result number are left shifted bits of the operands (bit shift left). |
>> | integer | Bits of the result number are right shifted bits of the operands (bit shift right). |
There math operators works with numeric data types (integer and float) and also with string. All are binary (except unary minus).
Operator | Datatype | Description |
---|---|---|
+ | integer, float, string | The result is sum of the numbers or concatenation of the strings. |
- | integer, float | The result is difference of the numbers. |
* | integer, float | The result is product of the numbers. |
/ | integer, float | The result is quotient of the numbers (number class is preserved, thus quotient of integers produce integer, etc). |
% | integer | The result is modulo. |
unary - | integer, float | The result is negative number. |
This is the operator known from C language ( condition ? expression : expression). The first operand is expression that can evaluate to boolean, types of second and third operands are code dependent. The result of the triple operator expression is the second operand in the case the first operand (condition) evaluates to true, the third one otherwise.
Code | Result | Comment |
---|---|---|
(3 > 2) ? true : false | true | The expression (3 > 2) evaluates to true, the result is true |
contains ([1, 2, 3], 5) ? "yes" : "no" | "no" | The expression contains ([1, 2, 3], 5) evaluates to false, the result is "no" |
(size ([]) > 0) ? 1 : -1 | -1 | The expression size ([]) > 0 evaluates to false, the result is -1 |
![]() | Note |
---|---|
Using brackets makes code cleaner, but is not necessary (according to operators precedence). |
![]() | Note |
---|---|
With the introduction of the index operator ( a = mapvar["key"]:default ), the sequence "]:" became a lexical token usable only for indexing, so watch out when using the triple operator with lists and maps. Use parentheses or white space. |
The table of operators precedence (from lowest to highest).
Direction | Operators |
---|---|
right | = |
left | ?: |
left | || |
left | && |
left | == != |
left | < <= > >= |
left | + - |
left | * / % |
left | << >> |
left | | |
left | & |
prefix | ! ~ - |
The bracket operator is the use of '[' and ']' like accessing arrays in C.
In YCP, this operator is used to ease handling with (possibly nested) lists and maps.
The bracket operator can be applied to any list or map variable and should be used in favour of (deeply) nested lookup() and select() cascades.
The access variant of the bracket operator is used for accessing elements of a list or a map. It effectively replaces select for lists and lookup for maps.
General syntax:
for simple lists:
<list-var>[<index>]:<default-value>
for nested lists:
<list-var>[<index>,<index> <, ...>]:<default-value>
index must be an integer and counts from 0 up to the number of elements-1.
It will return the default-value if you try to access an out-of-bounds element.
![]() | Note |
---|---|
Note that there must be no space between the closing bracket and the colon. |
Examples:
{ list l = [1, 2, 3]; integer three = l[2]:0; // == 3 integer zero = l[42]:0; // default value list ll = [[1,2], [3,4], [5,6]]; return (ll[1,0]:0 == three); // returns true } |
General syntax:
for simple maps:
<map-var>[<key>]:<default-value>
for nested lists:
<map-var>[<key>,<key> <, ...>]:<default-value>
key must have an allowed type for maps, integer, string, or symbol.
It will return default-value if you try to access an non existing key.
![]() | Note |
---|---|
Note that there must be no space between the closing bracket and the colon. |
Examples:
{ map m = $["a":1, "b":2, "c":3]; integer three = m["c"]:0; // == 3 integer zero = m["notthere"]:0; // default value map mm = $["a":$[1:2], "b":$[3:4], "c":$[5:6]]; return (mm["b",0]:0 == three); // returns true } |
Since the bracket operator applies to list and maps, you can use it to access nested lists and maps. But be careful not to mix up the index/key types.
Examples:
{ map map_of_lists = $["a":[1, 2, 3], "b":[4,5,6], "c":[7,8,9]]; integer three = map_of_lists["a",2]:0; // == 3 list list_of_maps = [$[1:2], $[3:4], $[5:6]]; return (list_of_maps[1,0]:0 == three); // returns true } |
The bracket operator can also be used on the left side of an assignment (lvalue).
This changes the list or map element in place (!!) and must be used with care.
If the map or list element does not exist, it will be created. The bracket operator can therefore replace add and change.
Creating a new list element will extend the size of the list. Holes will be filled with nil. See the examples below.
If used as an lvalue, the default value is not allowed.
Examples:
{ list l = [1,2,3]; // change the second element l[1] = 25; // l = [1,25,3] now ! // change the "c" element map m = $["a":1, "b":2, "c":3]; m["c"] = 42; // m = $["a":1, "b":2, "c":42] now // extend the list to 7 elements (0-6) l[6] = 6; // l = [1,25,3,nil,nil,nil,6] now ! // add a new element to m m["zz"] = 13; return (mm); // $["a":1, "b":2, "c":42, "zz":13] } |