Qore Programming Language Reference Manual  0.9.1
Operators

Table of Contents

The following table lists all Qore operators in order of precedence, starting with the highest precedence. The lower the precedence number, the higher the precedence, therefore the operators with precedence level 1 ("{}", "[]", ".") have the highest precedence of all Qore operators. The precedence levels in Qore are roughly equal to the precedence levels of C language operators. To explicitly specify the precedence for expression evaluation, use parentheses (). Note that all examples are given in %new-style.

Operators

Operator Prec. Description Example
`` 1 backquote/backtick operator
`ls -l`
{} 1 hash element or object member expression dereference operator
hash{"na" + "me"}
. 1 hash element or object member literal dereference operator
hash.name
obj.method()
[] 1 list element, string, and binary dereference operator
list[1]; string[3]; binary[2];
++ 2 pre-increment operator
++a
++ 2 post-increment operator
a++
-- 2 pre-decrement operator
--a
-- 2 post-decrement operator
a--
new 3 value instantiation/new operator
new Socket()
background 3 background/thread creation operator
background mainThread()
delete 3 delete operator
delete var
remove 3 remove operator
remove var
cast<>() 3 cast<>() operator
cast<SubClass>(var)
! 4 logical negation operator
if (!(a > 10)) {}
~ 5 binary not/bit inversion operator
var = ~var
+ (unary plus) 6 unary plus operator
var = +var
- (unary minus) 6 unary minus operator
var = -var
shift 7 shift list element operator
shift list
pop 7 pop list element operator
pop list
chomp 7 chomp end-of-line character operator
chomp string
trim 7 trim characters operator
trim string
elements 8 number of elements operator (list, hash, string, binary)
elements list
keys 8 hash key list operator
keys hash
* 9 multiplication operator
var = a * 10
/ 9 division operator
var = a / 10
% 10 modulo operator
var = a % 10
+ 11 plus operator: string, binary, list, and hash concatenation, integer and float addition
a + 10
"hello" + "there"
list + "new value"
hash + ( "newkey" : 100 )
- 11 minus operator (arithmetic subtraction, hash key removal)
a - 10
>> 12 bitwise shift right operator
0xff00 >> 8
<< 12 bitwise shift left operator
0xff00 << 8
exists 13 exists value operator
exists var
instanceof 13 instanceof operator
instanceof Qore::Mutex
< 14 Logical less than operator
a < 10
> 14 Logical greater than operator
a > 10
== 14 Logical equality operator
a == 10
!= 14 logical inequality operator
a != 10
<= 14 Logical less then or equals operator
a <= 10
>= 14 logical greater than or equals operator
a >= 10
<=> 14 logical comparison operator
a <=> b
=== 14 absolute logical equality operator
a === 10
!== 14 absolute logical inequality operator
a !== 10
=~ // 14 regular expression match operator
a =~ /text/
!~ // 14 regular expression no match operator
a !~ /text/
=~ s/// 14 regular expression substitution operator
a =~ s/text/text/
=~ x// 14 regular expression pattern extraction operator
a =~ x/(\w+):(\w+)/
=~ tr 14 transliteration operator
a =~ tr/a-z/A-Z/
& 15 bitwise/binary AND operator
a & 0xff
^ 16 bitwise/binary XOR operator
a ^ 0xff
| 17 bitwise/binary OR operator
a | 0xff
&& 18 logical AND operator
(a = 1) && (b < 10)
|| 19 logical OR operator
(a = 1) || (b < 10)
.. (range) 20 range operator
3..7
? : 21 conditional operator
a == 2 ? "yes" : "no"
?? 21 null coalescing operator
any var = a ?? b
?* 21 value coalescing operator
any var = a ?* b
, 22 comma operator
1, 2, 3, 4, 5
unshift 23 unshift list element operator
unshift list, val
push 23 push list element operator
push list, val
splice 23 splice list or string operator
splice list, 2, 2, (1, 2, 3)
extract 23 extract list or string operator
my sublist = extract list, 2, 2, (1, 2, 3)
map 23 map operator
map closure($1), list
hash map 23 hash map operator
map {expr($1) : expr($1)}, list
foldl 23 fold left to right operator
foldl closure($1 - $2), list
foldr 23 fold right to left operator
foldr closure($1 - $2), list
select 23 select elements from list operator
select list, $1 > 1
= 24 assignment operator
var = 1
:= 24 weak assignment operator
var := obj
+= 24 plus-equals (add-to) operator
var += 5
-= 24 minus-equals (subtract-from) operator
var -= 5
&= 24 and-equals operator
var &= 0x2000
|= 24 or-equals operator
var |= 0x2000
%= 24 modulo-equals operator
var %= 100
*= 24 multiply-equals operator
var *= 10
/= 24 divide-equals operator
var /= 10
^= 24 xor-equals operator
var ^= 0x2000
<<= 24 shift-left-equals operator
var <<= 0x2000
>>= 24 shift-right-equals operator
var >>= 0x2000

Operator Atomicity

All Qore operators perform thread-atomic actions with respect to the immediate arguments of the operator.

If more than one operator is used in an expression, the entire expression is not thread-atomic unless explicit user-level locking is used. For example: a += 5 is a thread-atomic action, but a += b– is not atomic, but rather made up of two atomic actions.

Operator Arguments

When an operator taking more than one argument is used with arguments of different data types, Qore automatically converts one or both data types to a data type supported by the operator in order to evaluate the result, according to the precedence lists given for each operator. That is; when an operator operates on mixed types, the types listed first in the following sections have precedence over types listed farther down in the lists. The result type will always be equal to the final operation type after any conversions due to type precedence per operator. If no type of either argument matches a supported data type for the operator, both types will be converted to the highest precedence data type for the operator and then the operator will evaluate the result. For explicit type conversion, please see the boolean(), string(), date(), int(), float(), etc functions.

Lazy Evalution of Functional Operators

Qore supports lazy evaluation of functional operators where possible in order to provide an efficient solution for processing large lists or nested functional operators.

The following operators support lazy evaluation directly:

  • map (non-hash version): allows the map expression to be evaluated in place when nested with other functional operators or used with a foreach statement
  • select: allows the filtered result list to be created and iterated in place when nested with other functional operators or used with a foreach statement rather than returning a temporary list
  • keys: allows the hash or object to be iterated in place when nested with other functional operators or used with a foreach statement rather than returning a temporary list of hash keys or accessible object members
  • .. (range operator) and [] with a range or multiple values: allow the operand to be iterated in place when nested with other functional operators or used with a foreach statement rather than returning a temporary list/string/binary object

The following operators and statements support lazy evaluation for iteration:

Example
With lazy functional evaluation, the following code does not create a potentially long itermediate list with the map expression, but rather the foldl expression will operate directly on the expressions generated when iterating the value returned by get_iterator():
# build a comma-separated string from an iterated expression
string str = foldl $1 + ", " + $2, (map $1.desc, get_iterator(), $1.type == External);

Operator Details


Backquote Operator (``)

Sandbox Restrictions
Qore::PO_NO_EXTERNAL_PROCESS
Synopsis
Executes the shell command in a separate process and returns the stdout as a string. To perform the same action using a Qore expression, see the backquote() function.
Syntax
`shell_command`
Return Type
string
Example
string dir = `ls -l`

Arguments Processed by ``

Argument Returns Processing
unquoted string shell_command string The shell command will be executed and the stdout is returned as a string
Exceptions
BACKQUOTE-ERRORAn error occurred in fork() or creating the output pipe

Hash Element or Object Member Expression Dereference Operator ({})

Synopsis
Retrieves the value of hash key or object member by evaulating an expression.
Syntax
container_expression { expression }
Return Type
any
Example
printf("%s\n", hash{getName()});

Arguments Processed by {}

Argument Processing
container_expression This expression must evaluate to a hash or an object; if not, the operator returns no value (NOTHING)
expression - list: if the expression evaluates to a list, then a slice of the hash or object is returned as a hash containing keys given in the list that are present in the hash or object. If the key as given in the list (converted to a string if necessary) is not present in the hash or object, then it is also not present in the hash returned; if none of the given keys exist in the hash, an empty hash is returned.
- anything other than list: the expression is converted to a string (if necessary); the value of the hash key corresponding to this string will be returned; if the key or member does not exist, then no value is returned
Exceptions
PRIVATE-MEMBERAttempt to access a private member outside the class

Hash Element or Object Member Literal Dereference Operator (.)

Synopsis
Retrieves the value of a hash key or object member using a literal identifier or an expression.
Syntax
hash_or_object_expression . identifier
object_expression . method_name([args ...])
hash_or_object_expression . expression
Return Type
any
Example
printf("%s\n", hash.name);
obj.method("argument");

Arguments Processed by . (hash key or object member literal dereference)

Argument Processing
hash_or_object_expression This expression must evaluate to a hash or an object; if not, then the operator returns no value
identifier An unquoted string taken as the literal name of the hash key or object member. If no such key exists, then no value is returned. In order to use hash keys that are not valid Qore identifiers, please use the {} operator. If the member is a private member and access is made outside the class, a run-time exception will be thrown. Also note that constants or static class member names will not be resolved, in this case the string given is used as the literal name of the hash key or object member

Arguments Processed by . (object method call)

Argument Processing
object_expression The object_expression must evaluate to an object or a run-time exception is thrown
method_name([args]) If the method does not exist in the class a run-time exception is thrown. Otherwise the method is called with any optional arguments given and the return value of the method is returned.

Arguments Processed by . (hash key or object member expression dereference)

Argument Processing
hash_or_object_expression This expression must evaluate to a hash or an object; if not, then the operator returns no value
expression - list: if the expression evaluates to a list, then a slice of the hash or object is returned as a hash containing keys given in the list that are present in the hash or object. If the key as given in the list (converted to a string if necessary) is not present in the hash or object, then it is also not present in the hash returned; if none of the given keys exist in the hash, an empty hash is returned.
- anything other than list: the expression is converted to a string (if necessary); the value of the hash key corresponding to this string will be returned; if the key or member does not exist, then no value is returned
Exceptions
PRIVATE-MEMBERAttempt to access a private member outside the class
METHOD-DOES-NOT-EXISTAttempt to access a method not defined for this class
METHOD-IS-PRIVATEAttempt to access a private method from outside the class
BASE-CLASS-IS-PRIVATEAttempt to access a method of a privately-inherited base class from outside the class
OBJECT-METHOD-EVAL-ON-NON-OBJECTAttempt to execute a method on a non-object

List, String, and Binary Dereference Operator ([])

Synopsis
Retrieves given list element(s), given character(s) of a string, or the integer value of given byte for a binary object or the binary object composed of given bytes of a binary object. If the index value is neither a list nor an integer it is converted to an integer. If the index is an integer and out of range, NOTHING is returned. If the index is a list and completely out of range, empty list/string/binary is returned. Note that this operator is not supported with multiple values and/or ranges between the square brackets on the left-hand side of an assignment expression.
Syntax
list_expression[index_expression]
string_expression[index_expression]
binary_expression[index_expression]
Return Type
any
Example
int n = (6,2,7,4)[2];
list l = (6,2,7,4)[3,1];
list l = (6,2,7,4)[2..0];
string s = "asdf"[2];
string s = "asdf"[3,1];
string s = "asdf"[1..];
int hex = <4e8a23fe>[2];
binary b = <4e8a23fe>[3,1];
binary b = <4e8a23fe>[..2];

Arguments Processed by []

Argument Processing
list_expression If the expression evaluates to a list, then the index_expression will be used to return the given element(s) from the list.
string_expression If the expression evaluates to a string, then the index_expression will be used to return the given character(s) from the string; note that multi-byte characters with UTF-8 are properly respected with this operator.
binary_expression If the expression evaluates to a binary, then the index_expression will be used to return the integer value of the given byte from the binary object or the binary object composed of the given bytes.
index_expression If the expression does not evaluate to a list or to an integer it is converted to an integer. Then the value(s) of the given element(s) are returned according to the type of the first expression (as listed above; elements start at position 0). If the expression is in the range form (e.g. [1..3], [2..]) then the dereference operator supports lazy functional evaluation.

This operator does not throw any exceptions; if the first expression does not evaluate to either a list, string, or binary, then no value (NOTHING) is returned.

Note
this operator cannot be used on the left-hand side of an assignment expression with multiple values between square brackets
Since
Qore 0.8.13 this operator accepts list expressions inside the square brackets

Pre-Increment Operator (++)

Synopsis
Increments an lvalue and returns the incremented value; only works on integer and floating-point values.
Syntax
++lvalue
Return Type
int or float
Example
++i;

Arguments Processed by ++ (pre-increement)

Argument Processing
Float increments lvalue and returns the result
Integer (or any other type) First converts the value of lvalue to an integer if necessary, then increments lvalue and returns the result

This operator does not throw any exceptions.


Integer Post-Increment Operator (++)

Synopsis
Increments an integer or floating-point lvalue and returns the value before the increment; if the lvalue is neither a floating-point value or an integer, it will be converted to an integer
Syntax
lvalue++
Return Type
any
Example
i++;

Arguments Processed by ++ (post-increment)

Argument Processing
Float saves the value of the lvalue as the result, then increments lvalue, then returns the saved original value of lvalue
Integer (or any other type) saves the value of the lvalue as the result, then converts the value of lvalue to an integer if necessary and increments it, then returns the saved original value of lvalue

This operator does not throw any exceptions.


Integer Pre-Decrement Operator (--)

Synopsis
Decrements an lvalue and returns the incremented value; only works on integer and floating-point values.
Syntax
lvalue
Return Type
int or float
Example
--i;

Arguments Processed by – (pre-decrement)

Argument Processing
Float increments lvalue and returns the result
Integer (or any other type) First converts the value of lvalue to an integer if necessary, then increments lvalue and returns the result

This operator does not throw any exceptions.


Integer Post-Decrement Operator (--)

Synopsis
Decrements an integer or floating-point lvalue and returns the value before the decrement; if the lvalue is neither a floating-point value or an integer, it will be converted to an integer
Syntax
lvalue
Return Type
any
Example
i–;

Arguments Processed by – (post-decrement)

Argument Processing
Float saves the value of the lvalue as the result, then decrements lvalue, then returns the saved original value of lvalue
Integer (or any other type) saves the value of the lvalue as the result, then converts the value of lvalue to an integer if necessary and decrements it, then returns the saved original value of lvalue

This operator does not throw any exceptions.


New Value Operator (new)

Sandbox Restrictions
Qore::PO_NO_NEW
Synopsis
This operator instantiates values according to the type and any argument(s) given.
Syntax
Return Type
  • If the type given is a class name or complex object type with a class name, then this operator returns an object of the specific class given
  • If the type given is a hashdecl hash type, then this operator returns a type-safe hash of the given type.
  • If the type given is a complex hash type, then this operator returns a hash with type-safe key values of the given type.
  • If the type given is a complex list type, then this operator returns a list with type-safe element values of the given type.
Examples
# create an object
Mutex obj = new Qore::Mutex();
# instantiate a new type-safe hash
hash<MyHash> h1 = new hash<MyHash>();
# instantiate a new hash with type-safe key values
hash<string, int> h2 = new hash<string, int>(("string": 2));
# instantiate a new list with type-safe element values
list<int> l1 = new list<int>(2);

Arguments Processed by new

Argument Processing
class_identifier([constructor_args, ...]) The class_identifier must be an existing class name or a namespace-prefixed path to an existing class; if so, the operator instantiates an object of this class, executes the constructor for the class (if any exists, along with any base class constructors, if applicable) on the new object, and returns the object (for constructor execution order in an inherited class, see Class Inheritance). If an exception is thrown in the constructor, the object is deleted immediately.
object<class_identifier>([constructor_args, ...]) same as above
hash<hashdecl_identifier>([hash_initializer_arg]) The hashdecl_identifier must be an existing declared type-safe hash name or a namespace-prefixed path to an existing type-safe hash; if so, the operator instantiates a hash of this type (see Type-Safe Hash Creation for more info).
hash<string, type>([hash_initializer_arg]) A value of the declared hash with type-safe key values is instantiated and returned
list<type>([list_initializer_args, ...]) A value of the declared list with type-safe key values is instantiated and returned
Note
  • When instantiating a class, see the class's documentation for possible exceptions
See also
Variable Implicit Construction for a more concise way to declare and initialize variables.

Background Operator (background)

Sandbox Restrictions
Qore::PO_NO_THREAD_CONTROL
Synopsis
Start a background thread and return the TID (thread ID).
Note
  • expressions that have no effect cannot be used with the background operator (a parse exception will be thrown)
  • it is illegal to make changes to a local variable anywhere in a background expression (a parse exception will be thrown)
  • local variables and find expressions are evaluated before the new thread is started and the result of the evaluation is used in the expression in the new thread
  • it is not possible to pass local variables by reference anywhere in a background expression (a parse exception will be thrown)
Syntax
background expression
Return Type
int
Example
int tid = background startThread();

Arguments Processed by background

Argument Processing
expression The expression given as an argument will be executed in a new thread. The TID of the new thread will be returned as the return value of the operator
Exceptions
THREAD-CREATION-FAILUREIf the thread table is full or if the operating system returns an error while starting the thread, this exception is thrown

Delete Operator (delete)

Synopsis
The delete operator deletes the contents of an lvalue. If the delete operator is called on an object, the object will be destroyed unconditionally. The delete operator does not return any value.

When called on a hash key, the key is removed from the hash entirely; when called on a list element, the element is assigned NOTHING (i.e. the list size does not change).

The delete operator will delete multiple keys from a hash (i.e. delete a slice from a hash) when called on a hash dereferenced by a list of strings, giving the keys to delete (see example below).

The delete operator will also remove characters from strings, bytes from binary objects, and elements from lists as well.

In the case the delete operator operates on an object, any exception can be thrown that is thrown by the class' destructor.

For a similar operator that returns the value that is removed from the data structure, and does not delete objects, see the remove operator.
Syntax
delete lvalue
Return Type
Does not return any value
Example
# delete a single key from a hash
delete value;
# delete multiple values from a hash
delete h.("a", "b", "c");
# delete characters from a string
delete str[1,2,5..7];
# delete bytes from a binary object
delete bin[2..4,10,12];
# delete elements from a list (if any of the referenced list elements are objects, they will be deleted as well)
delete l[2,5..7,8];

This operator does not throw any exceptions, however exceptions could be thrown in an object's destructor method when deleted by this operator.


Remove Operator (remove)

Synopsis
The remove operator removes a value from a data structure, or, in the case the operand of the remove operator is a simple value, the value itself is removed from the variable and returned. The remove operator returns the value removed from the lvalue.

When called on a hash key, the key is removed from the hash entirely, and the value returned is the value of the key removed from the hash; when called on a list element, the element is assigned NOTHING (i.e. the list size does not change).

The remove operator will remove and return a slice from a hash when called on a hash dereferenced by a list of strings, giving the keys to remove (see example below).

The remove operator will also remove characters from strings, bytes from binary objects, and elements from lists as well; when removing invalid or non-existant indices from strings and binary objects, no data is written to the output value, however in the case of a list lvalue, NOTHING is written to the output list in these cases. In case the same index is referenced multiple times, the value is repeated in the output value.

The remove operator does not call destructors when operating on objects, but if removing an object from an lvalue or from a data structure within the lvalue causes the object to go out of scope, it will be destroyed, and then its destructor could throw an exception.

For a similar operator that deletes the value that is removed from the data structure, see the delete operator.
Syntax
remove lvalue
Return Type
any
Example
# remove a single value from a hash
auto var = remove hash.value;
# remove a slice from a hash
hash nh = remove h.("a", "b", "c");
# remove a slice from a string
string nstr = remove str[1,2,5..7];
# remove a slice of bytes from a binary object
binary nbin = remove bin[2..4,10,12];
# remove a slice from a list (if any of the referenced list elements are objects, they will be deleted as well)
list nlist = remove l[2,5..7,8];

This operator does not throw any exceptions, however exception could be thrown in an object's destructor if it goes out of scope due to the action of this operator.


Cast Operator (cast<>())

Synopsis
The cast<>() operator provides a way to tell the parser that the type value is not actually the declared type but rather another compatible type as given between the angle brackets. Parse-time and runtime checks are performed for compatibility. Casts to hashdecl types, to the hash, and to the list types can result in data conversions; see below for more information.
Syntax
Return Type
  • If a class name is given as the target type, then this operator returns an object of the specific class given at runtime.
  • If object is given as the target type, the runtime type must be a valid object or a runtime exception will be raised; no conversion is performed on the object.
  • If a type-safe hash type is given as the target type, then this operator returns a hash of the given type at runtime; in this case the hash also undergoes conversion and initialization to the specified type; see Type-Safe Hash Creation for more information.
  • If a complex type is given as the target type, then this operator will convert the expression's value to the declared type at runtime if possible.
  • If hash is given as the target type, the runtime type must be a valid hash or a runtime exception will be raised, additionally, any type-safe information is removed and an untyped hash is returned
  • If list is given as the target type, the runtime type must be a valid list or a runtime exception will be raised, additionally, any type-safe information is removed and an untyped list is returned
Data Conversions with cast<>(...)
  • cast<hash<hashdecl_identifier>>(hash_expression): performs initialization of hashdecl keys with initializer expressions for keys not present in the output evaluated hash expression
  • cast<hash>(hash_expression): removes any type-safe information from the output evaluated hash expression
  • cast<list>(list_elements, ...): removes any type-safe information from the output evaluated list expression
Examples
# simple class cast
cast<SubClass>(obj).method();
# cast with complex object type (class cast)
cast<object<SubClass>>(obj).method();
# cast to object; ensures a valid object at runtime
cast<object>(obj);
# hashdecl cast with type conversion
hash<MyType1> h = cast<hash<MyType1>>(h2);
# complex hash cast
hash<string, MyClass>h = cast<hash<string, MyClass>(get_untyped_hash());
# removes key type information from a type-safe hash
any h = cast<hash>(new hash<MyHashdecl>());
# removes element type information from a type-safe list
any l = cast<list>(new list<int>(1));

Target Type Argument Processed by cast<>()

Argument Processing
class_identifier This must be a literal unquoted string giving a class name or a namespace-qualified path to a class (ex: Namespace::MyClass); a runtime check for compatibility with the target class is performed on the evaluated expression
object<class_identifier> this is equivalent to class_identifier alone; a runtime check for compatibility with the target class is performed on the evaluated expression
hashdecl_identifier This must be a literal unquoted string giving a type-safe hash name or a namespace-qualified path to a type-safe hash (ex: Namespace::MyHash); a runtime check for compatibility with the target hashdecl is performed on the evaluated expression; any keys missing from the expression that have default initializer expressions in the hashdecl are initialized accordingly
hash<string, data_type> The second type in the type parameter list gives the hash key value type; runtime type compatibility checks are performed but no conversions are made; a runtime exception will be thrown if the evaluated expression does not correspond to the declared type
list<data_type> The single type in the type parameter list gives the list element value type; runtime type compatibility checks are performed but no conversions are made; a runtime exception will be thrown if the evaluated expression does not correspond to the declared type
Exceptions
RUNTIME-CAST-ERRORThe expression given does not evaluate to a value that can be converted to the given type

Logical Not Operator (!)

Synopsis
Reverses the logical sense of an expression (True becomes False and False becomes True).
Syntax
!expression
Return Type
bool
Example
if (!exists error_code)
do_something();

Arguments Processed by !

Argument Processing
expression The expression is evaluated and converted to a bool, if necessary. Then the value is logically reversed (True becomes False, False becomes True)

This operator does not throw any exceptions.


Binary Not Operator (~)

Synopsis
The value of each bit in an integer is reversed (0 becomes 1, 1 becomes 0).
Syntax
~expression
Return Type
int
Example
a = ~b;

Arguments Processed by ~

Argument Processing
expression The argument is converted to an integer (if necessary), and bitwise negation is performed on the argument (ex: 666 & ~27 results in 640)

This operator does not throw any exceptions


Unary Minus Operator (-)

Synopsis
Changes the sign of numeric values.
Syntax
-expression
Return Type
int, number, float or date
Example
a = -b;

Arguments Processed by - (unary minus)

Argument Processing
Float Gives the negative of its argument as a Float, ex: -(-1.1) = 1.1, -(1.1) = -1.1
Integer Gives the negative of its argument as an Integer, ex: -(-1) = 1, -(1) = -1
Number Gives the negative of its argument as a Number, ex: -(-1.1n) = 1.1n, -(1.1n) = -1.1n
Date Gives the negative of its argument as a Date, ex: -(-1s) = 1s, -(1s) = -1s
other Returns 0

This operator does not throw any exceptions


Unary Plus Operator (+)

Synopsis
Does nothing with numeric values, converts others to 0.
Syntax
+expression
Return Type
int, number, float or date
Example
a = +b;

Arguments Processed by + (unary minus)

Argument Processing
Float Gives the value of its argument as a Float, ex: +1.1 = 1.1
Integer Gives the value of its argument as an Integer, ex: +1 = 1
Number Gives the value of its argument as a Number, ex: +1.1n = 1.1n
Date Gives the value of its argument as a Date, ex: +1s = 1s
other Returns 0

This operator does not throw any exceptions


Shift Operator (shift)

Synopsis
Removes the first element from a list and returns that element. Throws a parse-time exception if the lvalue is known not to be a list at parse time, otherwise throws a runtime exception if the lvalue is not a list if %strict-args is in effect.
Syntax
shift lvalue
Return Type
any
Example
*string a = shift ARGV;

Arguments Processed by shift

Argument Processing
lvalue Returns the first element of the list, and the list is modified by having the first element removed from the list. If the lvalue is an empty list, the operator returns no value (NOTHING).

This operator throws an exception if the lvalue is not a list.


Pop Operator (pop)

Synopsis
Removes the last element from a list and returns that element. Throws a parse-time exception if the lvalue is known not to be a list at parse time, otherwise throws a runtime exception if the lvalue is not a list if %strict-args is in effect.
Syntax
pop lvalue
Return Type
any
Example
a = pop list;

Arguments Processed by pop

Argument Processing
lvalue Returns the last element of the list, and the list is modified, having the last element removed from the list. If the lvalue is an empty list, the operator returns no value (NOTHING).

This operator throws an exception if the lvalue is not a list.


Chomp Operator (chomp)

Synopsis
Removes the end-of-line marker(s) ('\n' or '\r\n') from a string, or each string element in a list, or each hash key value in a hash (if the value is a string) and returns the number of characters removed.

To perform this operation on a non-lvalue expression, see the chomp() function.
Syntax
chomplvalue
Return Type
int
Example
chomp str;

Arguments Processed by chomp

Argument Processing
lvalue (String) Removes any EOL characters from a string and returns the number of characters removed.
lvalue (List) Removes any EOL characters from each string element of the list passed and returns the number of characters removed.
lvalue (Hash) Removes any EOL characters from each hash key's value (where the value is a string) and returns the number of characters removed.

This operator does not throw any exceptions.


Trim Operator (trim)

Synopsis
Removes whitespace characters from the beginning and end of a string, or each string element in a list, or each hash key value in a hash (if the value is a string) and returns the value processed (string, list, or hash).

To perform this operation on a non-lvalue expression, see the trim() function.

The following whitespace characters are removed from the beginning and end of strings: ' ' (blank spaces), '\n', '\r', '\t', '\v' (vertical tab, ASCII 11), and '\0' (null character).
Syntax
trim lvalue
Return Type
string, list, or hash
Example
trim str;

Arguments Processed by trim

Argument Processing
lvalue (String) Removes whitespace characters from the beginning and end of a string and returns the value processed.
lvalue (List) Removes whitespace characters from the beginning and end of each string element of the list passed and returns the list.
lvalue (Hash) Removes whitespace characters from the beginning and end of each string value of the hash passed and returns the hash.

This operator does not throw any exceptions.


Map Operator (map)

Synopsis
Executes (or maps) an expression on a list and returns the result. An optional select expression can be given to filter elements out from the result list.

If the first argument is a single element hash expression using curly brackets, then the hash variant of the map operator is used instead.

If the second argument (iterator_expression) is an object inheriting the AbstractIterator class, the map operator iterates the object by calling AbstractIterator::next(), and the implicit argument $1 in the map_expression and any optional select_expression is the value returned by AbstractIterator::getValue().

If the second argument is not a list or an object inheriting AbstractIterator, then map_expression is executed on the single value and the result is returned (assuming that any select_expression accepts the value).

If possible, iterator_expression is evaluated using lazy functional evaluation.
Return Type
any
Syntax
map map_expression, iterator_expression [, select_expression]
Examples
# returns (2, 4, 6)
list l = map $1 * 2, (1, 2, 3);
# prints out the keys of a hash; one to a line
map printf("%s\n", $1), hash.keyIterator();

Arguments Processed by map

Argument Processing
map_expression The expression to map on the list; the implicit argument $1 represents the current element being processed
iterator_expression The data to process; if this is not a list then the map_expression is run on the single argument passed, unless the iterator_expression is an object inheriting the AbstractIterator class; in this case the map operator iterates the object by calling AbstractIterator::next(); if supported, this expression is evaluated using lazy functional evaluation
[select_expression] An optional expression than can be used to filter out elements of the list before the map_expression is applied; if this expression evaluates to False on an element, then the element will be skipped and the map_expression will not be applied on that element.

This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).

See also
Hash Map Operator (map) for a variant of this operator that creates and returns a hash rather than a list
Since
Qore 0.8.6.2 the map operator when used with an AbstractIterator object instantiates the value returned by AbstractIterator::getValue() instead of the iterator itself
Note
the non-hash version of the map operator supports lazy functional evaluation of itself and also of the iterator_expression

Hash Map Operator (map)

Synopsis
This variant of the map operator builds a hash from a list or iterator argument and can be differentiated from the standard list map operator by the use of curly brackets in the initial expression; in this case the hash version of the map operator is used. This operator executes (or maps) an expression on a list and returns the result in form of a hash. An optional select expression can be given to filter elements out from the result hash.

If the first argument is anything other than a single element hash expression using curly brackets, then the list variant of the map operator is used instead.

If the third argument (iterator_expression) is an object inheriting the AbstractIterator class, the map operator iterates the object by calling AbstractIterator::next(), and the implicit argument $1 in the key_expression and value_expression and any optional select_expression is the value returned by AbstractIterator::getValue().

If the third argument is not a list or an object inheriting AbstractIterator, then key_expression and value_expression are executed on the single value and the result is returned as a single element hash (assuming that any select_expression accepts the value).

Return Type
Hash in case of a iterator_expression with a value, otherwise NOTHING
Syntax
map {key_expression : value_expression}, iterator_expression [, select_expression]
Examples
hash h = map {$1 : $1 * 2}, (1, 2, 3);
# returns: {"1": 2, "2": 4, "3": 6}
hash h = map {$1 : $1 * 2}, (1, 2, 3), $1 > 1;
# returns: {"2": 4, "3": 6}

Arguments Processed by the hash version of map

Argument Processing
key_expression The expression to map on the list or iterator value which will result in the key for the current hash element; the implicit argument $1 represents the current element being processed. The result of the expression is converted to a string to be used as the hash key for the current element
value_expression The expression to map on the list which will result in the value for the current hash element; the implicit argument $1 represents the current element being processed
iterator_expression The data to process; if this is not a list then the key_expression and value_expression are run on the single argument passed, unless the iterator_expression is an object inheriting the AbstractIterator class; in this case the map operator iterates the object by calling AbstractIterator::next(); if supported, this expression is evaluated using lazy functional evaluation
[select_expression] An optional expression than can be used to filter out elements of the list before the map expression is applied; if this expression evaluates to False on an element, then the element will be skipped and the key_expression and value_expression will not be applied on that element.

This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).

See also
Map Operator (map) for a variant of this operator that creates and returns a list
Since
Qore 0.8.12 this variant of the map operator exists which builds a hash from a list or iterator source

Fold Left Operator (foldl)

Synopsis
Folds an operation on a list from left to right and returns the result. The result of each individual operation is used as the first argument in the foldl expression for the next element in the list. The first operation of the fold is made by executing the fold expression on the first and second elements of the list, from this point onwards, the result of each successive operation is used as the first argument for the next operation, the second argument being the next element in the list.

If the second argument (iterator_expression) is an object inheriting the AbstractIterator class, the foldl operator iterates the object by calling AbstractIterator::next(), and the implicit arguments $1 and $2 in expression are the container values returned by AbstractBidirectionalIterator::getValue().

If the iterator_expression does not evaluate to a list or an object inheriting AbstractIterator, then the evaluated argument is returned immediately with no processing by the foldl expression.

If possible, iterator_expression is evaluated using lazy functional evaluation.
Syntax
foldl expression, iterator_expression
Return Type
any
Examples
The following returns foldl expression returns 5
int result = foldl $1 - $2, (10, 4, 1);

The following foldl expression joins a list with ", " or returns a single argument (if the second operand is not a list)
string str = foldl $1 + ", " + $2, list;

Arguments Processed by foldl

Argument Processing
expression The expression to fold on the list; the implicit argument $1 represents the result of the last operation (or the first element in the list when beginning the fold), and $2 represents the next element of the list.
iterator_expression The list, AbstractIterator object or other value according to the rules above to process; if possible, this expression is evaluated using lazy functional evaluation.

This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).

Note
the foldl operator supports lazy functional evaluation of the iterator_expression

Fold Right Operator (foldr)

Synopsis
Folds an operation on a list from right to left and returns the result. The result of each individual operation is used as the first argument in the foldr expression for the next element in the list in reverse order. The first operation of the right fold is made by executing the fold expression on the last and penultimate elements of the list, from this point onwards, the result of each successive operation is used as the first argument for the next operation, the second argument being the next element in the list in reverse order.

If the second argument (iterator_expression) is an object inheriting the AbstractBidirectionalIterator class, the foldr operator iterates the object by calling AbstractBidirectionalIterator::prev(), and the implicit arguments $1 and $2 in expression are the container values returned by AbstractBidirectionalIterator::getValue().

If the iterator_expression does not evaluate to a list or an object inheriting AbstractBidirectionalIterator, then the evaluated argument is returned immediately with no processing by the foldr expression.

If possible, iterator_expression is evaluated using lazy functional evaluation.
Syntax
foldr expression, iterator_expression
Return Type
any
Example
# returns -13
foldr $1 - $2, (10, 4, 1);

Arguments Processed by foldr

Argument Processing
expression The expression to fold on the list; the implicit argument $1 represents the result of the last operation (or the last element in the list when beginning the fold), and $2 represents the next element of the list in reverse order.
iterator_expression The list, AbstractIterator object or other value according to the rules above to process; if possible, this expression is evaluated using lazy functional evaluation.

This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).

Note
the foldr operator supports lazy functional evaluation of the iterator_expression

Select From List Operator (select)

Synopsis
Selects elements from a list that meet the given criteria and returns the new list.

If the first argument (iterator_expression) is an object inheriting the AbstractIterator class, the select operator iterates the object by calling AbstractIterator::next(), and the implicit argument $1 in expression is the value returned by AbstractIterator::getValue(); in this case the operator always returns a list (which may be empty if expression returns False for all elements in the iterator object or if the iterator object is empty), and the value of the object in the list returned is the value returned by AbstractIterator::getValue().

If the list expression does not evaluate to a list or an object inheriting AbstractIterator, then the select expression is evaluated using the value of the list expression as an argument, if it evaluates to True, then the value is returned, otherwise, no value is returned.

If possible, expression is evaluated using lazy functional evaluation.
Syntax
select iterator_expression, expression
Return Type
any
Example
# returns (2, 4, 6)
select (1, 2, 3, 4, 5, 6), !($1 % 2);

Arguments Processed by select

Argument Processing
iterator_expression The list to process or an object inheriting AbstractIterator
expression The expression will be evaluated on each element of the list, the implicit argument $1 represents current element of the list (or the iterator object itself when iterator_expression is an object inheriting AbstractIterator); only if the expression evaluates to True will the element appear in the result list

This operator does not throw any exceptions (however note that exceptions could be thrown by the expression executed by this operator).

Since
Qore 0.8.6.2 the map operator when used with an AbstractIterator object instantiates the value returned by AbstractIterator::getValue() instead of the iterator itself
Note
the select operator supports lazy functional evaluation of itself and also of the iterator_expression

Elements Operator (elements)

Synopsis
Returns the number of elements in a list, the number of keys in a hash, the number of characters (not bytes) in a string, or the number of bytes in a binary object.
Syntax
elements expression
Return Type
int
Example
int size = elements list;

Arguments Processed by elements

Argument Processing
expression list Returns the number of elements in the list
expression hash Returns the number of keys in the hash
expression string Returns the number of characters in the string (which may be different than the number of bytes for multi-byte character encodings such as UTF-8
expression binary Returns the number of bytes in the binary object

This operator does not throw any exceptions.

See also
<value>::size()

Keys Operator (keys)

Synopsis
Returns a list representing the keys in a hash.
Syntax
keys hash_expression
Return Type
list or NOTHING
Example
foreach string key in (keys hash)
printf("%s = %s\n", key, hash{key});

Arguments Processed by keys

Argument Processing
hash_expression Returns a list of strings giving the keys in hash_expression, which must evaluate to a hash. If not, then no value is returned.

This operator does not throw any exceptions.

See also

Multiply Operator (*)

Synopsis
Multiplies two arguments.
Syntax
expression1 * expression2
Return Type
int, float, or number
Example
value = x * y;

Arguments Processed by * (in order of precedence)

Argument Processing
number Gives the result of multiplying its arguments; ints and floats are converted to numbers if at least one of the arguments is a number
float Gives the result of multiplying its arguments; ints are converted to floats if at least one of the arguments is a float
int Gives the result of multiplying its arguments
any other type Converts argument to a number and performs the multiplication

This operator does not throw any exceptions.


Divide Operator (/)

Synopsis
Divides a number by another.
Syntax
expression1 / expression2
Return Type
int, float, or number
Example
value = x / y;

Arguments Processed by / (in order of precedence)

Argument Processing
number Gives the result of dividing its arguments; ints and floats are converted to numbers if at least one of the arguments is a number
float Gives the result of dividing its arguments; ints are converted to floats if at least one of the arguments is a float
int Gives the result of dividing its arguments
any other type Converts argument to a float and performs the division
Exceptions
DIVISION-BY-ZEROdivision by zero error

Modulo Operator (%)

Synopsis
Gives the integer remainder after division of one number by another.
Syntax
expression1 % expression2
Return Type
int
Example
mod = x % y;

Arguments Processed by %

Argument Processing
int Gives expression1 modulo expression2 (ex: 12 % 10 result in 2). Arguments are converted to integers if necessary.

This operator does not throw any exceptions.


Plus (Addition and Concatenation) Operator (+)

Synopsis
Numeric addition, list, string, binary, and hash concatenation operator.
Syntax
expression1 + expression2
Return Type
int, float, number, date, list, string, binary, or hash
Example
a = 1 + 2;
string = "hello" + "-there";
list = (1, 2) + ("three", "four", "five");
hash = ( "key1" : 1, "key2" : 2) + ( "key3" : "three", "key4": "four");
bin = bin1 + bin2;

Arguments Processed by + (in order of precedence)

Argument Processing
list list Gives the result of concatenating its arguments, i.e. (1, 2) + (3, 4) = (1, 2, 3, 4)
string Gives the result of concatenating its arguments
date Gives the result of adding date/time values (see Date/Time Arithmetic)
number Gives the result of adding its arguments
float Gives the result of adding its arguments
int Gives the result of adding its arguments
hash Gives the result of concatenating/merging its arguments. Any common keys will be overwritten by the values in the second hash (expression2)

This operator does not throw any exceptions.


Minus Operator (-)

Synopsis
With float, integer, or number arguments, subtracts one number from another.

With date arguments, subtracts one date from another; if both date arguments are absolute dates, the result is a relative date (duration) giving the time between them; if the first date argument is an absolute date and the second is a relative date (duration), then the result is an absolute date. If both date arguments are relative dates, then the result is a relative date. If the first argument is a relative date and the second date is an absolute date, the absolute date's epoch offset (offset in seconds and microseconds from 1970-01-01Z) is used to perform the calculation, and a relative date/time value is produced.

However, if the left-hand side is a hash, and the right-hand side is a string, then the result will be a new hash with the hash key represented by the string removed. If the left-hand side is a hash and the right-hand side is a list, then a new hash will be produced and each element from the list will be converted to a string key and any hash item with a matching key will be deleted from the new hash. Having both left and right arguments hashes is an invalid operation which does not modify the hashes in any way and silently returns NOTHING.
Syntax
expression1 - expression2
Return Type
int, float, number, date, or hash
Example
num = x - y;
date = 2010-05-13 - P3MT14H10M;
hash = hash - "key";
hash = hash - ("key1", "key2", "key3");

Arguments Processed by - (in order of precedence)

Argument Processing
date date subtraction: expression1 - expression2
number arithmetic subtraction: expression1 - expression2
float arithmetic subtraction: expression1 - expression2
int arithmetic subtraction: expression1 - expression2
hash - string hash key deletion: expression1 - expression2
hash - list hash key deletion: expression1 - expression2; all elements of the list are converted to strings (if necessary) and any keys with those names are deleted from the hash.
hash - hash no-op; silently returns NOTHING

This operator does not throw any exceptions.


Shift Right Operator (>>)

Synopsis
Shifts bits in an integer towards zero (divides an integer by a power of 2)
Syntax
expression1 >> expression2
Return Type
int
Example
a = x >> y;

Arguments Processed by >>

Argument Processing
int Gives the result of shifting expression1 right by expression2 bits. Arguments are converted to integers if necesssary.

This operator does not throw any exceptions.


Shift Left Operator (<<)

Synopsis
Shifts bits in an integer towards infinity (multiplies an integer by a power of 2)
Syntax
expression1 << expression2
Return Type
int
Example
a = x << y;

Arguments Processed by <<

Argument Processing
int Gives the result of shifting expression1 left by expression2 bits. Arguments are converted to integers if necessary.

This operator does not throw any exceptions.


Operator (instanceof)

Synopsis
Tests if an expression is an instance of the given type or not, meaning that the type can accept the value.
Syntax
expression instanceof type_identifier
Return Type
bool
Example
if (obj instanceof Qore::Mutex)
print("object is Mutex\n");

Arguments Processed by instanceof

Argument Processing
expression If expression is an instance of the named type, which is defined as the type is able to accept the value, then the operator returns True, otherwise returns False.

This operator does not throw any exceptions.


Exists Operator (exists)

Synopsis
Tests if an expression represents a value or not.
Syntax
exists expression
Return Type
bool
Example
if (exists a)
printf("a = n\n", a);

Arguments Processed by exists

Argument Processing
expression If expression evaluates to a value, then the operator returns True, otherwise returns False.

This operator does not throw any exceptions.


Less Than Operator (<)

Synopsis
Tests if a value is less than another; types are converted if necessary (ex: ("1" < 2) is True).
Syntax
expression1 < expression2
Return Type
bool
Example
if (x < y)
printf("%n is less than %n\n", x, y);

Arguments Processed by < (in order of precedence)

Argument Processing
number If expression1 is numerically less than expression2, returns True, otherwise returns False
float If expression1 is numerically less than expression2, returns True, otherwise returns False
int If expression1 is numerically less than expression2, returns True, otherwise returns False
string If expression1 comes before expression2 in string sort order, returns True, otherwise returns False
date If expression1 is before (or a shorter amount of time than of the arguments are Relative Date/Time Values (Durations)) expression2, returns True, otherwise returns False

This operator does not throw any exceptions.


Greater Than Operator (>)

Synopsis
Tests if a value is greater than another; types are converted if necessary (ex: ("2" > 1) is True).
Syntax
expression1 > expression2
Return Type
bool
Example
if (x > y)
printf("%n is less than %n\n", x, y);

Arguments Processed by > (in order of precedence)

Argument Processing
number If expression1 is numerically greater than expression2, returns True, otherwise returns False
float If expression1 is numerically greater than expression2, returns True, otherwise returns False
int If expression1 is numerically greater than expression2, returns True, otherwise returns False
string If expression1 comes after expression2 in string sort order, returns True, otherwise returns False
date If expression1 is after expression2, returns True, otherwise returns False

This operator does not throw any exceptions.


Equals Operator (==)

Synopsis
Tests if a value is equal to another; types are converted if necessary (ex: ("1" == 1) is True). For absolute equals, where types must also be equal to return true, see the Absolute Equals Operator (===).
Syntax
expression1 == expression2
Return Type
bool
Example
if (x == y)
printf("%n is equal to %n\n", x, y);

Arguments Processed by == (in order of precedence)

Argument Processing
string If expression1 is equal to expression2, returns True, otherwise False
number If expression1 is equal to expression2, returns True, otherwise False
float If expression1 is equal to expression2, returns True, otherwise False
int If expression1 is equal to expression2, returns True, otherwise False
date If expression1 is equal to expression2, returns True, otherwise False
list If each element in the each list where order is relevant satisfies this operator, the operator returns True, otherwise it returns False
hash If each hash has the same keys and the value of each equal key in each hash satisfies this operator, the operator returns True, otherwise it returns False
binary If expression1's memory contents and size are equal to expression2's, then returns True, otherwise False
object If expression1 is a reference to the same object as expression2, then returns True, otherwise False
NULL If both expressions are NULL, returns True, otherwise returns False
NOTHING If neither expression has a value, returns True, otherwise returns False

This operator does not throw any exceptions.


Not Equals Operator (!=)

Synopsis
Tests if a value is not equal to another; types are converted if necessary (ex: ("1" != 1) is False).
Syntax
expression1 != expression2
Return Type
bool
Example
if (x != y)
printf("%n is not equal to %n\n", x, y);

Arguments Processed by != (in order of precedence)

Argument Processing
string If expression1 is not equal to expression2, returns True, otherwise False
number If expression1 is not equal to expression2, returns True, otherwise False
float If expression1 is not equal to expression2, returns True, otherwise False
int If expression1 is not equal to expression2, returns True, otherwise False
date If expression1 is not equal to expression2, returns True, otherwise False
list If each element in the each list where order is relevant satisfies this operator, the operator returns True, otherwise it returns False
hash If the hashes have different keys or the value of each equal key in each hash satisfies this operator, the operator returns True, otherwise it returns False
binary If expression1's memory contents or size are not equal to expression2's, then returns True, otherwise False
object If expression1 is not a reference to the same object as expression2, then returns True, otherwise False
NULL If either expressions is not NULL, returns True, otherwise returns False
NOTHING If one of the expressions has a value, returns True, otherwise returns False

This operator does not throw any exceptions.


Less Than Or Equals Operator (<=)

Synopsis
Tests if a value is less than or equals to another value; types are converted if necessary (ex: ("1" <= 2) is True).
Syntax
expression1 <= expression2
Return Type
bool
Example
if (x <= y)
printf("%n is less than or equal to %n\n", x, y);

Arguments Processed by <= (in order of precedence)

Argument Processing
number If expression1 is numerically less than or equal to expression2, returns True, otherwise returns False
float If expression1 is numerically less than or equal to expression2, returns True, otherwise returns False
int If expression1 is numerically less than or equal to expression2, returns True, otherwise returns False
string If expression1 comes before in string sort order or is the same as expression2, returns True, otherwise returns False
date If expression1 is before or is the same exact date and time as expression2, returns True, otherwise returns False

This operator does not throw any exceptions.


Greater Than Or Equals Operator (>=)

Synopsis
Tests if a value is greater than or equals to another value; types are converted if necessary (ex: ("2" >= 1) is True).
Syntax
expression1 >= expression2
Return Type
bool
Example
if (x >= y)
printf("%n is greater than or equal to %n\n", x, y);

Arguments Processed by >= (in order of precedence)

Argument Processing
number If expression1 is numerically greater than or equal to expression2, returns True, otherwise returns False
float If expression1 is numerically greater than or equal to expression2, returns True, otherwise returns False
int If expression1 is numerically greater than or equal to expression2, returns True, otherwise returns False
string If expression1 comes after in string sort order or is the same as expression2, returns True, otherwise returns False
date If expression1 is after or is the same exact date and time as expression2, returns True, otherwise returns False

This operator does not throw any exceptions.


Comparison (<=>) Operator

Synopsis
Tests if the left-hand value is less than, equal, or greater than the right-hand value; types are converted if necessary (ex: ("1" <=> 2) returns -1).
Syntax
expression1 <=> expression2
Return Type
int
Example
switch (x <=> y) {
case -1:
print("x is less than y\n");
break;
case 0:
print("x is equal to y\n");
break;
case 1:
print("x is greater than y\n");
break;
}

Arguments Processed by <=> (in order of precedence)

Argument Processing
string If expression1 comes after in string sort order as expression2, returns 1, otherwise if they are equal, returns 0, otherwise if expression1 comes before expression2, returns -1
number If expression1 is numerically greater than expression2, returns 1, otherwise if they are equal returns 0, otherwise returns -1
float If expression1 is numerically greater than expression2, returns 1, otherwise if they are equal returns 0, otherwise returns -1
int If expression1 is numerically greater than expression2, returns 1, otherwise if they are equal returns 0, otherwise returns -1
date If expression1 is after expression2, returns 1, otherwise if they are equal returns 0, otherwise returns -1

This operator throws NAN-COMPARE-ERROR if any of the operands is NaN.


Absolute Equals Operator (===)

Synopsis
Checks two values for equality without doing any data type conversions; if the types do not match, then the result is False.
Syntax
expression1 === expression2
Return Type
bool
Example
if (x === y)
printf("%n is equal to %n and has the same data type as well\n", x, y);

Arguments Processed by ===

Argument Processing
All This operator returns True only if the types and values of both sides of the operator are exactly equal, otherwise returns False. No type conversions are done.

This operator does not throw any exceptions.


Absolute Not Equals Operator (!==)

Synopsis
Checks two values for inequality without doing any data type conversions. If the data types do not match, then returns True.
Syntax
expression1 !== expression2
Return Type
bool
Example
if (x !== y)
printf("%n is not equal to %n and may not have the data type as well\n", x, y);

Arguments Processed by !==

Argument Processing
All This operator returns True if either the types or the values of the arguments are different, otherwise it retuns False. No type conversions are done.

This operator does not throw any exceptions.


Regular Expression Match Operator (=~)

Synopsis
Checks for a regular expression match; returns True if the expression matches the string, False if not. See Qore Regular Expression Operator Options for the meaning of the i, s, x, and m options after the regular expression.

See Regular Expressions for more information about regular expression support in Qore.
Syntax
expression =~ [m]/regex/[isxm]
Return Type
bool
Example
if (str =~ /hello/)
printf("%s contains 'hello'\n", str);

Arguments Processed by =~

Argument Processing
string This operator returns True if the regular expression in regex matches the string in expression.

This operator does not throw any exceptions.


Regular Expression No Match Operator (!~)

Synopsis
Checks for a regular expression non match; returns True if the expression does not match the string, False if it does. See Qore Regular Expression Operator Options for the meaning of the i, s, x, and m options after the regular expression.

See Regular Expressions for more information about regular expression support in Qore.
Syntax
expression !~ [m]/regex/[isxm]
Return Type
bool
Example
if (str !~ /hello/)
printf("%s does not contain 'hello'\n", str);

Arguments Processed by !~

Argument Processing
string This operator returns True if the regular expression in regex does not match the string in expression.

This operator does not throw any exceptions.


Regular Expression Substitution Operator

Synopsis
Looks for a regular expression match in a string, and, if found, substitutes the matched string with a new string. Subpattern backreferences are supported in the target string, $1=first subpattern, $2=second subpattern, etc... See Qore Regular Expression Operator Options for the meaning of the i, s, x, and m options after the regular expression.

See Regular Expressions for more information about regular expression support in Qore.
Syntax
lvalue =~ s/regex_pattern/target_string/[isxmg]
Return Type
string or NOTHING (if the lvalue does not hold a string)
Example
str =~ s/hello/goodbye/i;
str =~ s/(\w+) +(\w+)/$2, $1/;

Arguments Processed by =~ s///

Argument Processing
string This operator substitutes text in the lvalue string if the regular expression matches. Subpattern backreferences are supported in target_string, $1=first subpattern, $2=second subpattern, etc..

This operator does not throw any exceptions.


Regular Expression Pattern Extraction Operator

Synopsis
Matches regular expression patterns (enclosed in parentheses) in a string and returns a list giving the text matched for each pattern. If the regular expression does not match, then no value (NOTHING) is returned. See Qore Regular Expression Operator Options for the meaning of the i, s, x, and m options after the regular expression.

See Regular Expressions for more information about regular expression support in Qore.
Syntax
string =~ x/regex_with_patterns/[isxm]
Return Type
list or NOTHING (if the lvalue does not hold a string or if the pattern is not matched)
Example
list =~ x/(\w+):(\w+)/;
list =~ x/(.*)\.(.*)/;

Arguments Processed by =~ x//

Argument Processing
string This operator extracts strings from string based on patterns enclosed in parentheses in the regular expression.

This operator does not throw any exceptions.


Transliteration Operator

Synopsis
Makes character substitutions in an lvalue; character ranges can also be used.
Syntax
lvalue =~ tr/source_chars/target_chars/
Return Type
string or NOTHING (if the lvalue does not hold a string)
Example
str =~ tr/a-z/A-Z/;

Arguments Processed by =~ tr//

Argument Processing
string This operator substitutes characters in the lvalue string. Note that if there are more characters in source_chars than in target_chars, then the last character in target_chars will be used for any source matches where the source character position is greater than the length of target_chars.

This operator does not throw any exceptions.


Bitwise/Binary And Operator (&)

Synopsis
Performs a bitwise (binary) AND operation on two integers.
Syntax
expression1 & expression2
Return Type
int
Example
a = x & y;

Arguments Processed by &

Argument Processing
int Gives the result of the binary (bitwise) AND operation between expression1 and expression2 (ex: 0xffb2 & 0xa1 = 0xa1); operands are converted to integers if necessary.

This operator does not throw any exceptions.


Bitwise/Binary Or Operator (|)

Synopsis
Performs a bitwise (binary) OR operation on two integers.
Syntax
expression1 | expression2
Return Type
int
Example
a = x | y;

Arguments Processed by |

Argument Processing
int Gives the result of the binary (bitwise) OR operation between expression1 and expression2 (ex: 0xb001 | 0xfea = 0xbfeb); operands are converted to integers if necessary

This operator does not throw any exceptions.


Bitwise/Binary Xor Operator (^)

Synopsis
Performs a bitwise (binary) XOR operation on two integers.
Syntax
expression1 ^ expression2
Return Type
int
Example
a = x ^ y;

Arguments Processed by ^

Argument Processing
int Gives the result of the binary (bitwise) EXCLUSIVE OR operation between expression1 and expression2 (ex: 0xaef1 & 0xfb32 = 0x55c3); operands are converted to integers if necessary

This operator does not throw any exceptions.


Logical And Operator (&&)

Synopsis
Checks to see if two expressions are True with logical short-circuiting.
Syntax
expression1 && expression2
Return Type
bool
Example
if (x && y)
printf("%n and %n are both True\n", x, y);

Arguments Processed by &&

Argument Processing
bool Returns True if both expressions are True, False if otherwise. Logical short-circuiting is implemented; if expression1 is False, then expression2 is not evaluated, and the operator returns False.

This operator does not throw any exceptions.


Logical Or Operator (||)

Synopsis
Returns True if either of the arguments are True with logical short-circuiting.
Syntax
expression1 || expression2
Return Type
bool
Example
if (x || y)
printf("either %n or %n or both are True\n", x, y);

Arguments Processed by ||

Argument Processing
bool Returns True if either or both expressions evaluate to True, False if otherwise. Logical short-circuiting is implemented; if expression1 is True, then expression2 is not evaluated, and the operator returns True.

This operator does not throw any exceptions.


Conditional Operator (? :)

Synopsis
Evaluates and returns the value of one of two expressions depending on the value of a conditional expression.
Syntax
expression ? if_true_expression : if_false_expression
Return Type
any
Example
a = (z > 100 ? "Big" : "Small");

Arguments Processed by ? :

Argument Processing
All If expression is evaluated to be True, then the if_true_expression is evaluated and returned. Otherwise the if_false_expression is evaluated and returned.

This operator does not throw any exceptions.


Null Coalescing Operator (??)

Synopsis
Evaluates the first operand and checks for a value (i. e. not NOTHING or NULL). If it evaluates to a value, then the value of the first operand is returned, otherwise the second operand is evaluated and returned. When chained, the first argument with a value is returned.
Syntax
expression1 ?? expression2
Return Type
any
Example
x = a ?? b ?? c;

Arguments Processed by ??

Argument Processing
All If expression1 is evaluated to be NOTHING or NULL then the expession2 is evaluated and returned. Otherwise the value of expression1 is returned.

This operator does not throw any exceptions.

See also Value Coalescing Operator (?*) that works same except for operands are evaluated based on being False instead of being NOTHING or NULL.


Value Coalescing Operator (?*)

Synopsis
Evaluates the first operand and checks for a value (True or False). If it evaluates to True, then the value of the first operand is returned, otherwise the second operand is evaluated and returned. When chained, the first argument with a value is returned.
Syntax
expression1 ?* expression2
Return Type
any
Example
x = a ?* b ?* c;

Arguments Processed by ?*

Argument Processing
All If expression1 is evaluated to be False then the expession2 is evaluated and returned. Otherwise the value of expression1 is returned.

This operator does not throw any exceptions.

See also Null Coalescing Operator (??) that works same except for operands are evaluated based on being NOTHING or NULL instead of being False.


Comma Operator (,)

Synopsis
Makes a list from more than one element.
Syntax
expression1, expression2
Return Type
list
Example
a = 1, 2, "three";

Arguments Processed by ,

Argument Processing
All The comma operator builds lists of arguments

This operator does not throw any exceptions.


Unshift Operator (unshift)

Synopsis
Inserts an element into the first position of a list, moves all other elements up one position and returns the list processed. Throws an exception if the lvalue is not a list.
Syntax
unshift lvalue, expression
Return Type
list
Example
unshift list, "one";

Arguments Processed by unshift

Argument Processing
All Inserts the value of expression as the first element in the list given by lvalue. All other elements in the list are moved forward. If expression evaluates to a list, this list will be appended as the last element of lvalue. To concatenate lists, use the plus operator.

Push Operator (push)

Synopsis
Adds one element to the end of a list and returns the list processed. Throws a parse-time exception if the lvalue is known not to be a list at parse time, otherwise throws a runtime exception if the lvalue is not a list if %strict-args is in effect.
Syntax
push lvalue, expression
Return Type
list
Example
push list, "last";

Arguments Processed by push

Argument Processing
All Appends the value of the expression as the last element in the list given by lvalue. If expression evaluates to a list, this list will be appended as the last element of lvalue. To concatenate lists, use the plus operator.

Splice Operator (splice)

Synopsis
Removes and optionally inserts elements in lists, strings, and binary objects and returns the lvalue after processing. For a similar operator that returns the values removed, see the extract operator.

Works on strings, lists, and binary data in a similar way; removes elements from a list, characters from a string, or bytes from binary data and optionally inserts new ones. If no length_expression is given, splice removes all elements/characters/bytes from the list, string, or binary data lvalue starting at offset_expression (offsets begin at 0). Otherwise, a number of elements/characters/bytes equal to length_expression is removed (or up to the end of the list/string/data if applicable). If substitution_expression is present, then the removed elements/characters/bytes are substituted with the elements/string/bytes given by this expression.

Note that string splice takes character offsets, which may not be the same as byte offsets for multi-byte character encodings, such as UTF-8
Syntax
splice lvalue, offset_expression, [length_expression, [substitution_expression]]
Return Type
list, string, or binary (returns lvalue after processing)
Example
splice list, 2, 2;
splice string, 2, 2, "-text-";
splice bin, 2, 2, <deadbeef>;

Arguments Processed by splice

Argument Processing
lvalue (list, string, or binary) If the lvalue is a list, list elements are processed, if it is a string, characters in the string are processed, and for binary data, bytes are processed. For any other data type, no action is taken.
offset_expression The start element/character/byte position for removing elements/characters/bytes from the list, string, or binary data; if this value is negative, it gives the element offset from the end of the data
length_expression The number of elements/characters/bytes to remove. If this expression is not present, then all elements/characters/bytes from the offset to the end of the list/string/binary data are removed. If this expression is present and evaluates to 0, no characters/elements/byte are removed; if this value is negative, then it gives an offset from the end of the data (ie -2 means remove all elements/characters/bytes up to but not including the last two)
substitution_expression For list splice, an optional element or list to substitute for the removed elements (to insert a list in a single element's position, make sure that the list to be inserted is the first and only element of another list used as the argument in this position; in other words, pass a list within a single-element list). For string splice, an optional string to substitute for the removed characters. For binary splice, string or binary data to substitute for any removed bytes.

Extract Operator (extract)

Synopsis
Removes and optionally inserts elements in lists and strings. For a similar operator that removes values from an lvalue and returns the lvalue (instead of the value removed), see the splice operator.

Works on either strings, lists, and binary data in a similar way; removes elements from a list, characters from a string, and bytes from binary data and optionally inserts new ones. If no length_expression is given, extract removes all elements/characters/bytes from the lvalue starting at offset_expression (offsets begin at 0). Otherwise, a number of elements/characters/bytes equal to length_expression is removed (or up to the end of the data if applicable). If substitution_expression is present, then the removed elements/characters/bytes are substituted with the data given by this expression.

When operating on lists, a list is returned of any elements extracted (if no elements are extracted, then an empty list is returned); when operating on strings, a string is extracted of all characters extracted from the string (if no characters are extracted, then an empty string is returned). When operating on binary data, a binary object is returned.

Note that string extract takes character offsets, which may not be the same as byte offsets for multi-byte character encodings, such as UTF-8
Syntax
extract lvalue, offset_expression, [length_expression, [substitution_expression]]
Return Type
list, string, or binary (the value(s) removed from lvalue)
Example
list sublist = extract list, 2, 2;
string substring = extract string, 2, 2, "-text-";
binary b = extract bin, 2, 2, <deadbeef>;

Arguments Processed by extract

Argument Processing
lvalue (list, string, or binary) If the lvalue is a list, list elements are processed, if it is a string, characters in the string are processed, and for binary data, bytes are processed. For any other data type, no action is taken.
offset_expression The start element/character/byte position for removing elements/characters/bytes from the list, string, or binary data; if this value is negative, it gives the element offset from the end of the data
length_expression The number of elements/characters/bytes to remove. If this expression is not present, then all elements/characters/bytes from the offset to the end of the list/string/binary data are removed. If this expression is present and evaluates to 0, no characters/elements/byte are removed; if this value is negative, then it gives an offset from the end of the data (ie -2 means remove all elements/characters/bytes up to but not including the last two)
substitution_expression For list extract, an optional element or list to substitute for the removed elements (to insert a list in a single element's position, make sure that the list to be inserted is the first and only element of another list used as the argument in this position; in other words, pass a list within a single-element list). For string splice, an optional string to substitute for the removed characters. For binary splice, string or binary data to substitute for any removed bytes.

Assignment Operator (=)

Synopsis
Assigns a value to an lvalue and returns the value assigned.
Syntax
lvalue = expression
Return Type
any
Example
a = 1;

Arguments Processed by =

Argument Processing
All Assigns the value of expression to lvalue

Weak Reference Assignment Operator (:=)

Synopsis
Identical to the assignment operator except when the rvalue is an object; in such cases a weak reference to the object is assigned instead of a strong reference. Weak references do not extend the life of an object and also do not participate in object graph cycles (i.e. recursive references) and therefore can be used to break up large graphs of objects to improve garbage collector performance with such graphs, however the programmer must ensure that the object has a valid strong reference elsewhere or the object will go out of scope.

This operator should only be used in very special circumstances by expert users and is only allowed when the %allow-weak-references sandbox permission is set.
Syntax
lvalue := rvalue_expression
Return Type
any
Example
object a := get_object_from_cache();

Arguments Processed by :=

Argument Processing
All Assigns the value of expression to lvalue and returns the value assigned; if rvalue_expression is an object, a weak reference is assigned instead of a strong reference

Plus Equals Operator (+=)

Synopsis
Increments and concatenates an lvalue with the value of an expression depending on the data type of the lvalue, unless the lvalue is NOTHING, in which case this operator acts like the assignment operator (simply assigns the value of the right hand side to the lvalue).
Syntax
lvalue += expression
Return Type
int, float, number, date, list, string, binary, hash, or object
Example
a += 10;
date += P1M2DT45M;
list += new_element;
string += ".foo";
binary += <0c67a374>
hash += ("new-key" : 1, "other" : "two");
object += hash;

Arguments Processed by +=

Argument Processing
lvalue (list) the expression will be evaluated and concatenated to the lvalue. If expression is a list, the lists will be concatenated, to ensure adding a single element to a list, use the push operator
lvalue (hash or (object) the expression will be evaluated, and, if it is a hash or object, then it's members will be added to the lvalue, any duplicate elements in the lvalue will be overridden by elements in the expression.
lvalue (string) the expression will be evaluated and converted to a string if necessary and concatenated to the lvalue.
lvalue (number) the expression will be evaluated and converted to a number if necessary and added to the lvalue.
lvalue (float) the expression will be evaluated and converted to a float if necessary and added to the lvalue.
lvalue (binary) the expression will be evaluated and converted to a binary if necessary and added to the lvalue.
lvalue (date) the expression will be evaluated and converted to a date if necessary and added to the lvalue.
lvalue (NOTHING) the lvalue will be assigned to the value of expression.
lvalue (all other types) the lvalue's type will be converted to an integer, and the expression will be evaluated and converted to an integer if necessary, and then the result will be added to the lvalue.

Minus Equals Operator (-=)

Synopsis
For a float or integer argument, decrements the value of an lvalue by the value of an expression. However if the lvalue is a hash or object and the expression is a string, removes the key represented by the string from the hash or object.
Syntax
lvalue -= expression
Return Type
int, float, number, date, hash, or object
Example
a -= 10;
date -= PT45H213S;
hash -= "key";
hash -= ("key1", "key2");
object -= "key";
object -= list_of_keys;

Arguments Processed by -=

Argument Processing
lvalue (number) the expression will be evaluated and converted to a number if necessary and subtracted from the lvalue
lvalue (float) the expression will be evaluated and converted to a float if necessary and subtracted from the lvalue
lvalue (date) the expression will be evaluated and converted to a date if necessary and subtracted from the lvalue
lvalue (hash or (object), expression (string) the hash key represented by expression will be removed from the lvalue
lvalue (hash or (object), expression (list) each element in the list will be converted to a string (if necessary) and the key represented by each string will be removed from the hash or object
lvalue (NOTHING), expression (any type) the expression will be assigned to lvalue
lvalue (all other types) the lvalue's type will be converted to an integer (if necessary), and the expression will be evaluated and converted to an integer (if necessary), and then the result will be subtracted from the lvalue

And Equals Operator (&=)

Synopsis
Performs a bitwise (binary) AND operation on an lvalue using the value of an expression and returns the new value.
Syntax
lvalue &= expression
Return Type
int
Example
a &= 0xfe;

Arguments Processed by &=

Argument Processing
All the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be binary and'ed to the lvalue

Or Equals Operator (|=)

Synopsis
Performs a bitwise (binary) OR operation on an lvalue using the value of an expression and returns the new value.
Syntax
lvalue |= expression
Return Type
int
Example
a |= 0xba;

Arguments Processed by |=

Argument Processing
All the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be binary or'ed to the lvalue

Modulo Equals Operator (%=)

Synopsis
Performs a modulo calculation on an lvalue using the value of an expression and returns the new value.
Syntax
lvalue %= expression
Return Type
int
Example
a %= 100;

Arguments Processed by %=

Argument Processing
All the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be used to divide the lvalue's value and the remainder will be saved to the lvalue

Multiply Equals Operator (*=)

Synopsis
Performs a multiplication operation on an lvalue using the value of an expression and returns the value assigned.
Syntax
lvalue *= expression
Return Type
int, float, or number
Example
a *= 10;

Arguments Processed by *=

Argument Processing
All The type precedence from highest to lowest is number, float float, and int, other types are converted to int. The expression will be evaluated and multiplied by the lvalue, and the result will be saved to the lvalue.

Divide Equals Operator (/=)

Synopsis
Performs a division operation on an lvalue using the value of an expression and returns the value assigned.
Syntax
lvalue /= expression
Return Type
int, float, or number
Example
a /= 10;

Arguments Processed by /=

Argument Processing
All The type precedence from highest to lowest is number, float float, and int, other types are converted to int; The expression will be evaluated and multiplied by the lvalue, and the result will be saved to the lvalue. The expression will be evaluated and used to divide the lvalue, and the result will be saved to the lvalue.
Exceptions
DIVISION-BY-ZEROIf the divisor expression evaluates to zero, this exception is thrown

Xor Equals Operator (^=)

Synopsis
Performs an exclusive-or operation on an lvalue using the value of an expression.
Syntax
lvalue ^= expression
Return Type
int
Example
a ^= 0xf9034ba7;

Arguments Processed by ^=

Argument Processing
All Values are converted to integers if necessary. The expression will be evaluated and exclusive-or'ed with the lvalue, and the result will be saved to the lvalue

Shift Left Equals Operator (<<=)

Synopsis
Performs a shift-left operation on an lvalue using the value of an expression and returns the value assigned.
Syntax
lvalue <<= expression
Return Type
int
Example
a <<= 3;

Arguments Processed by <<=

Argument Processing
All Values are converted to integers if necessary. The expression will be evaluated and this value will determine how many bits the lvalue will be shifted left. The result will be saved to the lvalue.

Shift Right Equals Operator (>>=)

Synopsis
Performs a shift-right operation on an lvalue using the value of an expression and returns the value assigned.
Syntax
lvalue >>= expression
Return Type
int
Example
a >>= 3;

Arguments Processed by >>=

Argument Processing
All Values are converted to integers if necessary. The expression will be evaluated and this value will determine how many bits the lvalue will be shifted right. The result will be saved to the lvalue.

Range Operator (..)

Synopsis
Returns a list of integers between two expressions.
Syntax
expression1 .. expression2
Return Type
list
Example
list = 3..7;
list = 7..3;

Arguments Processed by ..

Argument Processing
int Arguments are converted to integers if necessary.
Since
Qore 0.8.13
Note
  • the range operator supports lazy functional evaluation of itself
  • this operator cannot be used on the left-hand side of an assignment expression