Programmer Guide > Working with Lists and Associative Arrays > Defining an Associative Array
  

Defining an Associative Array
Use the ASARR function to create an associative array. You can call ASARR in the following two ways:
result = ASARR(key1, expr1 , ..., keyn, exprn)
where key1, expr1 , ..., keyn, exprn are pairs of key names (strings) and expressions or variables. A key name is a string that uniquely identifies the expression or variable that immediately follows.
result = ASARR(keys_arr, values_list)
where keys is an array of key names (strings) and values is a list array containing the expressions and/or variables. The first element in the keys array is paired with (and uniquely identifies) the first element in the values array, and so on.
Example 1
An associative array is created using the first form of the ASARR function. Key names and values are specified as separate parameters.
as = ASARR('byte', 1B, 'float', 2.2, 'string', $
'3.3', 'struct', {,a:1, b:lindgen(2)})
Example 2
An associative array, equivalent to the array in Example 1, is created using the second form of the ASARR function, described previously. An array of key names is created first, followed by an array of values. Note that the values do not have to be of the same data type.
as=ASARR(['byte', 'float', 'string', 'struct'], $
LIST(1B, 2.2, '3.3', {,a:1, b:lindgen(2)}))
The INFO command shows the contents of the associative array.
INFO, as, /Full
; PV-WAVE prints the following:
; AS  AS. ARR  = Associative Array(4)
; byte  BYTE  = 1
; struct  STRUCT  = ** Structure $3, 2 tags, 12 length:
; A INT  1
; B LONG  Array(2)
; float  FLOAT  = 2.20000
; string  STRING  = '3.3'
The PRINT command also shows the contents of the array.
PRINT, as
; PV-WAVE prints the following:
; {'byte' 1 'struct'{ 1 0 1} 'float' 2.20000 'string'3.3 }

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.