3.6. YCP Operators

As any other programming language YCP knows a lot of operators that can be used to act on data.

3.6.1. Comparison Operators

These are binary operators for comparison of two values. The result is always boolean.

OperatorDatatypeDescription
==almost allTrue if operands are equal, otherwise false.
<almost allTrue if left operand is smaller than the right one, otherwise false.
>almost allTrue if left operand is greater than the right one, otherwise false.
<=almost allTrue if left operand is smaller or equal to the right one, otherwise false.
>=almost allTrue if left operand is greater or equal to the right one, otherwise false.
!=almost allTrue if operands are not equal, otherwise false.

3.6.2. Boolean Operators

These are logical operators, that works with boolean datatype, two are binary one is unary. The result is always boolean.

OperatorDatatypeDescription
&&booleanTrue if both operands are true, otherwise false (logical and).
||booleanTrue if at least one of the operands is true, otherwise false (logical or).
!booleanTrue if the operand if false, otherwise false (logical not).

3.6.3. Bit Operators

These are bit operators that works with integer, two are binary one is unary. The result is always integer.

OperatorDatatypeDescription
&integerBits of the result number are product of the bits of the operands (bit and).
|integerBits of the result number are count of the bits of the operands (bit or).
~integerBits of the result number are reverted bits of operand (bit not).
<<integerBits of the result number are left shifted bits of the operands (bit shift left).
>>integerBits of the result number are right shifted bits of the operands (bit shift right).

3.6.4. Math Operators

There math operators works with numeric data types (integer and float) and also with string. All are binary (except unary minus).

OperatorDatatypeDescription
+integer, float, stringThe result is sum of the numbers or concatenation of the strings.
-integer, floatThe result is difference of the numbers.
*integer, floatThe result is product of the numbers.
/integer, floatThe result is quotient of the numbers (number class is preserved, thus quotient of integers produce integer, etc).
%integerThe result is modulo.
unary -integer, floatThe result is negative number.

3.6.5. Triple Operator

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.

CodeResultComment
(3 > 2) ? true : falsetrueThe 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-1The expression size ([]) > 0 evaluates to false, the result is -1
[Note]Note

Using brackets makes code cleaner, but is not necessary (according to operators precedence).

[Note]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.

3.6.6. Operators Precedence

The table of operators precedence (from lowest to highest).

 Direction  Operators 
right=
left?:
left||
left&&
left== !=
left< <= > >=
left+ -
left* / %
left<< >>
left|
left&
prefix! ~ -

3.6.7. The bracket operator

3.6.7.1. Introduction

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.

3.6.7.2. Access variant

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.

3.6.7.2.1. Accessing lists

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

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
}

3.6.7.2.2. Accessing maps

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

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
}

3.6.7.2.3. Mixed map/list access

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
}

3.6.7.3. Assign variant

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]
}