Syntax
The syntax of a subscript reference is:
variable_name(subscript_list)
Or:
(array_expression)(subscript_list)
The subscript list is simply a list of expressions, constants, or subscript ranges which contains the values of the one or more subscripts. Subscript expressions are separated by commas if there is more than one subscript. In addition, multiple elements are selected with subscript expressions that contain either a contiguous range of subscripts or an array of subscripts.
Subscript Reference Discussion
Subscripts may be used either to retrieve the value of one or more array elements or to designate array elements to receive new values. The expression:
ARR(12)
denotes the value of the thirteenth element of ARR (because subscripts start at 0), while the statement:
ARR(12) = 5
stores the number 5 in the thirteenth element of ARR without changing the other elements.
Elements of multidimensional arrays are specified by using one subscript for each dimension. With PV‑WAVE, like with FORTRAN, the first subscripts vary fastest in memory.
If A is a 2-by-3 array, the command: PRINT,A prints the array like this:
A0,0 A1,0
A0,1 A1,1
A0,2 A1,2
On the other hand, the command: PM,A prints the array like this:
A0,0 A0,1 A0,2
A1,0 A1,1 A1,2
But regardless of how the array is printed, the values are stored in memory in the same way: A0,0, A1,0, A0,1, A1,1, A0,2, A1,2. As another example, suppose B is a 2 by 2 by 2 array. Then, B is stored in memory in the order:
B0,0,0, B1,0,0, B0,1,0, B1,1,0, B0,0,1, B1,0,1, B0,1,1, B1,1,1
Elements of multidimensional arrays may also be specified using only one subscript, in which case the array is treated as a 1D array with the same number of elements. For instance, in the previous examples, A(2) is the same element as A(0,1), A(5) is the same element as A(1,2), and B(5) is the same as B(1,0,1).
If an attempt is made to reference a non-existent element of an array using a scalar subscript (a subscript that is negative or larger than the size of the dimension minus 1), an error occurs and program execution stops.
Subscripts may be any type of array or scalar expression. If a subscript expression is not of type integer, a longword integer copy is made and used to evaluate the subscript. For example:
A(1.4) = A(1.6) = A(1)
Arrays (as well as scalars) can be assigned to an array element referenced by a scalar subscript. For instance: A(S)=ARR, where ARR is an array. In this case, the elements of ARR are stored sequentially into A beginning at the element A(S).
Examples
a = INDGEN( 5 ) & a(2) = [ 10, 20 ] & PRINT, a
a = LONARR( 4, 5 ) & a(5) = REPLICATE( 1, 6 ) & PM, a
a = LONARR( 4, 5 ) & a(1,1) = REPLICATE( 1, 2, 3 ) & PM, a
Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.