Qore supports three types of container types: lists, hashes (associative arrays), and objects (see Objects and Classes for more information on objects and classes). These container types can be combined to make arbitrarily complex data structures.
The data type of any element can be any basic type or another aggregate type. The types do not have to be uniform in one container structure.
Lists (or arrays) are simply ordered containers of values. A list element can be any Qore type (even another list, hash, or object).
Lists are specified by giving expressions separated by commas as follows:
$list =expression
,expression
[,expression
...];
Here is a concrete example:
$list = 1, 2, "three", 4.0, 5, 6;
Note that trailing commas can be left on the end of a list (or a hash, for that matter). This makes it easier to comment-out the last element of a multi-line list without having to worry about removing the trailing comma.
List elements are dereferenced using square brackets: "[" and "]". The first element in a list has index zero.
$element3 = $list[2];
The following operators perform special processing on lists: elements, shift, unshift, push, pop, splice, [], +, +=, map, foldl, foldr, and select.
Hashes are containers that associate values to a string key.
Note that Qore hashes preserve the insertion order in order to be able to guarantee the order of keys when hashes are serialized to XML strings (see XML Integration), therefore the keys operator will always return the hash keys in insertion/creation order.
Hashes are specified using the following syntax:
$hash = ( "key1" :expression
, "key2" :expression
, ... );
Here is a concrete example:
$hash = ( "apple" : 1 + 1, "pear" : "good" );
Hashes are dereferenced in one of two ways, either using curly brackets: "{" and "}", where any valid Qore expression can be used, or using the dot "." hash member dereferencing operator, where only literal strings can be used.
$element3 = $hash{"pe" + "ar"};
Is equivalent to:
$element3 = $hash.pear;
and:
$element3 = $hash."pear";
A literal string after the dot "." hash member dereferencing operator must be a valid Qore identifier; therefore if you want to use a hash key that's not a valid identifier, enclose the string in quotes.
If you want to use the result of an expression to dereference the hash, then the curly bracket syntax must be used.
Note that hash keys can also be given by constants (as long as the constant resolved to a string).
Qore objects are instantiations of a class. They have members (like a hash - values associated to string keys), and methods. The class definition specifies the methods that run on objects of that class, private members, and static methods associated with the class (however note that static methods do not run in the scope of an object). Qore classes are declared with a special syntax, and objects are instantiated using the new operator as follows.
newclass_identifier
([argument list
])
For example:
my $m = new Mutex();
Objects have named data members that are referenced like hash elements, although this behavior can be modified for objects using the memberGate() method. Object members are accessed by appending a dot '.' and the member name to the object reference as follows:
object_reference.member_name
For more information, see Object Members.
Object methods are called by appending a dot '.' and a method name to the object reference as follows:
object_reference
.method_name
([argument_list
])
Or, from within the class code itself to call another method from inside the same class hierarchy:
$.method_name
([argument_list
])
For more information, see Object Method Calls.
The object references above are normally variable references holding an object, but could be any expression that returns an object, such as a new expression or even a subroutine call.
Objects are treated differently than other Qore data types; they are only explicitly copied (see Object References for more informaion). Any object instantiated with the new operator will remain unique until deleted or explicitly copied. An explicit copy is made with the copy method, and does not always guarantee an exact copy of the source object (it depends on the definition of the copy method for the class in question).
Objects exist until they go out of scope, are explicitly deleted, or their last thread exits. For detailed information, see the section Classes on Qore classes.