The READ procedures perform formatted input into variables.

READ performs input from the standard input stream (IDL file unit 0), while READF requires a file unit to be explicitly specified.

Examples


To read a string value into the variable B from the keyboard, enter:

; Define B as a string before reading:
B = ''
; Read input from the terminal:
READ, B, PROMPT='Enter Name: '

To read formatted data from the previously-opened file associated with logical unit number 7 into variable C, use the command:

READF, 7, C

The following code snippet reads multiple lines from a text file and stores them in a string array:

; Select a text file and open for reading
file = DIALOG_PICKFILE(FILTER='*.txt')
OPENR, lun, file, /GET_LUN
; Read one line at a time, saving the result into array
array = ''
line = ''
WHILE NOT EOF(lun) DO BEGIN & $
  READF, lun, line & $
  array = [array, line] & $
ENDWHILE
; Close the file and free the file unit
FREE_LUN, lun

Syntax


READ, [Prompt,] Var1, ..., Varn

READF, [Prompt,] Unit, Var1, ..., Varn

Keywords: [, AM_PM=[string, string]] [, DAYS_OF_WEEK=string_array{7 names}] [, FORMAT=value] [, MONTHS=string_array{12 names}] [, PROMPT=string]

Arguments


Prompt

Tip: The PROMPT keyword should be used instead of the Prompt argument for compatibility with the IDL Workbench.

A string or explicit expression (i.e, not a named variable) to be used as a prompt. This argument should not be included if the FORMAT keyword is specified. Also, if this argument begins with the characters “$(”, it is taken to be a format specification as described below under “Format Compatibility”.

Using the Prompt argument does not work well with the IDL Workbench. The desired prompt string is written to the log window instead of the command input window. To create custom prompts compatible with the IDL Workbench, use the PROMPT keyword, described below.

Unit

For READF, Unit specifies the file unit from which the input is taken.

Vari

The named variables to receive the input.

Note: Vari may not be an element of an array variable (arrayvar[n]) or a field within a structure variable (structvar.tag).

If the variable specified for the Vari argument has not been previously defined, the input data is assumed to be of type float, and the variable will be created as a float.

Keywords


AM_PM

Supplies a string array of two names to be used for the names of the AM and PM string when processing explicitly formatted dates (CAPA, CApA, and CapA format codes) with the FORMAT keyword.

DAYS_OF_WEEK

Supplies a string array of 7 names to be used for the names of the days of the week when processing explicitly formatted dates (CDWA, CDwA, and CdwA format codes) with the FORMAT keyword.

FORMAT

If FORMAT is not specified, IDL uses its default rules for formatting the input. FORMAT allows the format of the input to be specified in precise detail, using a FORTRAN-style specification. See Using Formatted Input/Output.

Note: The format specification affects the way data is read from the input stream or file. It does not change the datatype of the variables into which data is being read.

MONTHS

Supplies a string array of 12 names to be used for the names of the months when processing explicitly formatted dates (CMOA, CMoA, and CmoA format codes) with the FORMAT keyword.

PROMPT

Set this keyword to a scalar string to be used as a customized prompt for the READ command. If the PROMPT keyword or Prompt argument is not supplied, IDL uses a colon followed by a space (“: ”) as the input prompt.

Format Compatibility


If the FORMAT keyword is not present and READ is called with more than one argument, and the first argument is a scalar string starting with the characters “$(”, this initial argument is taken to be the format specification, just as if it had been specified via the FORMAT keyword. This feature is maintained for compatibility with version 1 of VMS IDL.

Version History


Original

Introduced

Pre-6.2

Deprecated KEY_ID, KEY_MATCH, and KEY_VALUE keywords

See Also


READS, READU, WRITEU , General File Access