# READ DATA Read Data from Program

> Source: https://manual.pvxplus.com/directives/read-data

## Formats

|  |  |
| --- | --- |
| 1\. [Read Data](https://manual.pvxplus.com/directives/read-data#Mark6)*:* | **READ DATA** *varlist*,...\[,**ERR**=*stmtref*\] |
| 2\. [Read from String:](https://manual.pvxplus.com/directives/read-data#Mark8) | **READ DATA FROM** *data$* \[*,fileopt*\] **TO** *varlist* |

***Where:***

|  |  |
| --- | --- |
| *data$* | Data to be parsed into the *varlist*. String expression. |
| *fileopt* | Supported file options (see **[File Options](https://manual.pvxplus.com/appendix/input-output-and-control-options#Mark1)**):<br>\|  \|  \|<br>\| --- \| --- \|<br>\| **ERR**=*stmtref* \| Error transfer \|<br>\| **REC**=*name$* \| Record prefix (REC=VIS(*string$***)** can also be used where *string$* contains the record prefix.) \|<br>\| **SEP**=*char$* \| Default separator character to parse data. Hex or ASCII string value. Dynamic **SEP=\*** separators are ***not*** supported. \|<br>\| If the **READ DATA** statement also contains a **REC=** clause, the **REC=** clause must precede the **SEP=** clause. \|  \| |
| *stmtref* | Program line number or statement label to which to transfer control. |
| *varlist* | List of variables to receive the values from the **DATA** statements. Comma-separated numeric and string variables are allowed. |

## Description

Use the **READ DATA** directive to read data embedded in a program.

## Format 1

***Read Data***

**READ DATA** *varlist*\[,ERR=*stmtref*\]

Use this format to transfer the values/expressions from data statements in a program to the variables identified in *varlist*. When **READ DATA** is executed, PxPlus evaluates each expression in the data statements in order and places the values into the corresponding variables defined in your **READ DATA** directive.

PxPlus increments an internal pointer to the next data expression during the reading of *data$*. When the end of a data statement is reached, the next data statement in the program is used. Use a **RESTORE** directive to reset the pointer to the start of the first data statement.

***Example 1:***

Include the **END=** or **ERR=** option to avoid an Error #2 when the pointer is at end of file:

```
0010 data "Dog","Cat","Pig"
0020 data "Pig","Cat","Dog"
0030 read data X$, end=1000
0040 print X$," | ",
0050 goto 0030
1000 restore
1010 print "DONE"; stop

->begin
->run
Dog | Cat | Pig | Pig | Cat | Dog | DONE
```

## Format 2

***Read Data from String***

**READ DATA FROM** *data$***\[,REC**=*string$***\]\[SEP**=*char$***\] TO** *varlist*\[,ERR=*stmtref*\]

You can **READ DATA FROM** a string to initialize a list of variables or to parse a record into different IOLists.

***Example 2 - Initialize Variables:***

You can **READ DATA *to*** an IOList to initialize all variables:

```
0010 ! Clear all variables in an IOLIST
0100 read data from "" to iol=8000
8000 iolist ID$,NAME$,AMT

0010 read data from "",rec=WORK$ to iol=iol(WORK)
```

***Example 3 - Parse Record:***

You can use **READ DATA** to parse a record into different IOLists:

```
0100 read record (1)R$
0200 if R$(1,1)="T" then read data from R$ to iol=8000
0200: else read data from R$ to iol=8010
8000 iolist TYPE$,TRN_DATE$,TRN_AMOUNT
8010 iolist TYPE$,PAY_DATE$,PAY_AMOUNT
```

## See Also

[DATA Define Data Elements](https://manual.pvxplus.com/directives/data)  
[RESTORE Reset Program Data Position](https://manual.pvxplus.com/directives/restore)  
[BEGIN Reset Files and Variables](https://manual.pvxplus.com/directives/begin)  
[LOAD Read Program into Memory](https://manual.pvxplus.com/directives/load)
