MATREADCSV

MATREADCSV

Top  Previous  Next

 

The MATREADCSV statement reads a CSV format line of text from a directory file record previously opened for sequential access and parses it into the elements of a dimensioned matrix.

 

 

Format        

 

MATREADCSV matrix {ENCODING name} FROM file.var {DELIMITER delim}

{THEN statement(s)}

{ELSE statement(s)}

 

where

 

matrixis the dimensioned matrix to receive the data read from the file.

 

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.

 

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

 

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, placing the elements into successive elements of the matrix. 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 elements of the matrix will be set to null strings. If the line of text has more data items than the number of elements in the matrix, the excess data is placed in the zero element as for MATPARSE. The INMAT() function can be used to determine the number of data items in exactly the same way as for MATREAD.

 

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 matrix will be unchanged.

 

The CSV rules are described under the WRITECSV statement.

 

 

Example

 

DIM DETAILS(10)

LOOP

  MATREADCSV DETAILS FROM DELIVERY.F 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 into elements of the DETAILS matrix. 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, NOBUF, OPENSEQ, READBLK, READSEQ, SEEK, WEOFSEQ, WRITEBLK, WRITECSV, WRITESEQ, WRITESEQF