READCSV

READCSV

Top  Previous  Next

 

The READCSV statement reads a CSV format line of text from a directory file record previously opened for sequential access and parses it into multiple variables.

 

 

Format

 

READCSV {ENCODING name} FROM file.var {DELIMITER delim} TO var1, var2,...

{THEN statement(s)}

{ELSE statement(s)}

 

where

 

encodingis the ECS character encoding to be used for the data.

 

file.varis the file variable associated with the record by a previous OPENSEQ statement.

 

delimis the delimiter separating data elements. This defaults to a comma if omitted. Only the first character of delim is significant.

 

var1, var2 ...are the variables to receive the data read from the file.

 

statement(s)are statement(s) to be executed depending on the outcome of the READCSV.

 

At least one of the THEN and ELSE clauses must be present.

 

 

The optional ENCODING element to this statement sets the character encoding to be used, overriding any encoding set in the VOC F-type record or when the file was opened.

 

A line of text is read from the file. It is then parsed according to the CSV format rules as defined in RFC 4180, placing the elements into the data items identified by var1, var2, etc. If successful, the THEN clause is executed and the STATUS() function would return zero.

 

If there are fewer data items in the line of text than the number of variables supplied, the remaining variables will be set to null strings. If the line of text has more data items than the number of variables supplied, the excess data is ignored.

 

If there are no further fields to be read, the ELSE clause is executed and the STATUS() function would return ER$RNF (record not found). The target variables will be unchanged.

 

The CSV rules are described under the WRITECSV statement.

 

 

Example

 

LOOP

  READCSV FROM DELIVERY.F TO PROD.NO, QTY ELSE EXIT

  GOSUB PROCESS.DELIVERY.DETAILS

REPEAT

 

This program fragment reads CSV format lines of text from the record open for sequential access via the DELIVERY.F file variable, placing the elements of the line into PROD.NO and QTY. It then calls the PROCESS.DELIVERY.DETAILS subroutine to process the new item. The loop terminates when the ELSE clause is executed when all fields have been processed.

 

 

See also:

CLOSESEQ, CREATE, CSVDQ(), DPARSE.CSV, NOBUF, OPENSEQ, READBLK, READSEQ, SEEK, WEOFSEQ, WRITEBLK, WRITECSV, WRITESEQ, WRITESEQF