Using References
Reference Operator: \A
The reference operator returns a reference to the value of «A». «A» is an expression which is evaluated in the current context. «A» reference acts as an atomic element for array abstraction, and can be thought of as a pointer to a value (the value is often array-valued). with the simple syntax, \A
, all dimensions of «A» are placed under the reference.
A reference displays in a result table as «ref». You can view the value pointed to by the reference by double-clicking on the cell containing «ref».
\[I, J]A
A list of zero or more indexes can be specified with taking the reference of a value. When indexes are specified, only those dimensions are swallowed by the reference. If there are other dimensions, the result returned will be an array of references. An empty list of references, \[]A
, creates pointers to atomic values.
Dereference Operator: #R
When you have a reference (e.g., a pointer to a value), the dereference operator, #R
, returns the value pointed to.
The operator has a lower precedence than subscript, so that #R[I = x]
is equivalent to #(R[I = x])
. In other words, when you have an array of references indexed by I
, this is how you get to the thing pointed to by one of those references. If you need to slice the de-referenced value, then use (#R)[I = x]
.
When «R» is an array of references, #R
will contain the union of all indexes from the de-referenced cells. There are many cases where you want to avoid this, either because the resulting number of dimensions would be huge, or because two or more of the dereferenced values contain an implicit dimension (e.g., list), which would cause an error. Because references are often used to separate items with distinct dimensionality, or to store separate lists, these situations are common for cases where references are used. Thus, you will often need to explicitly loop with For..Do or other looping constructs, rather than letting array abstraction take care of it for you.
Comparisons
Less-than and greater-than comparisons are not defined for references and always return Null.
Testing equality of references gets a bit tricky, and in general it is a bad practice to rely on equality of references. References are equal only if they point to the exact same data in memory. The value pointed to by two references can be identical while the references are not considered equal. For example:
\"Hello" = \"Hello" → 0 { they point to two different instances of "Hello" in memory }
Local s := "Hello" Do (\s = \s) → 1 { Now they point to the same instance in memory }
References to equal numbers, null, or undef do test for equality.
\1 = \1 → 1
\Null = \Null → 1
Subscripting on a reference does not work in Analytica 4.1 and earlier, even if the references are considered equal by the equality operator. For example, A[J = Slice(J, 1)]
will not get the first slice in A
when the first element of J
is a reference. This is remedied in Analytica 4.2.
Enable comment auto-refresher