PV-WAVE Foundation > Programmer Guide > Working with Lists and Associative Arrays > Lists, Associative Arrays Input and Output
Lists, Associative Arrays Input and Output
To read and write lists and associative arrays, use the procedures READ, READF, PRINT, and PRINTF. Lists and associative arrays are transferred in much the same way as simple data types, with each element of the list or associative array transferred in order.
Writing a List or Associative Array with PRINT or PRINTF
You can write a list or an associative array with PRINT or PRINTF. By default, each element of the list or associative array is output in the default format of the element's data type. The entire list or associative array is enclosed in { } (braces). Each value of an associative array element is preceded by the key name enclosed in ” ” (double quotes).
Example
The contents of an associative array are printed with the PRINT command. This example uses the same code used to demonstrate nesting.
; Define a data structure to hold variables and attributes.
strDef = {Main_Data_Str, attrs:ASARR(), vars: LIST()}
; Create an empty associative array that will hold the GUI tool 
; data structure. This array will be filled in later, possibly 
; in another procedure.
dataStr = ASARR()
; Call the tool Wg1 and set a size attribute for the Wgl tool.
dataStr('Wg1') = {Main_Data_Str}
dataStr('Wg1').attrs('size') = [512, 512]
; Set a list of variables for the Wg1 tool. 
dataStr('Wg1').vars = LIST('VAR1', 'VAR2')
; Print the contents of the associative array.
PRINT, dataStr
; PV-WAVE prints: {'Wg1'{{'size' 512 512}{ VAR1 VAR2}}}
Reading a List or Associative Array with READ or READF
You can read data into an associative array or list using the READ or READF function. This is similar to reading data into a structure; however, associative array elements must be read in a particular order. You can determine this order with the ASKEYS function, as shown in the following example:
; Create an associative array.
as = ASARR('byte', 1B, 'float', 2.2, 'string', $
'hello', 'struct', {,a:1, b:lindgen(2)})
; Get the correct order of elements in the associative array. 
; Note that the order is different than the order of the 
; parameters in the ASARR function used to create the array.
PRINT, ASKEYS(as) 
; PV-WAVE prints: byte struct float string
; Read in some data. Input to READ and READF must be separated 
; by at least one space.
READ, as
: 4 5 6 7 8.8 hello
PRINT, as
; PV-WAVE prints: {”byte”   4 ”struct”{   5   6   7 } 
; PV-WAVE prints: ”float”     8.80000 ”string” hello}