# MATREAD

Updated: 10/19/2021, 9:29:48 PM
Created: 10/19/2021, 9:29:48 PM
Last Updated By: Kevin Munoz
Read Time: 2 minute(s)

Tags: dimensioned array record handling

# Description

The MATREAD statement allows a record stored in a jBASE file to be read and mapped directly into a dimensioned array. It takes the general form:

MATREAD array FROM {variable1,}expression {SETTING setvar}  {ON ERROR statements} {LOCKED statements} {THEN|ELSE statements}
1

Where:

  • array should be a previously dimensioned array, which will be used to store the record to be read.
  • If specified, variable1 should be a jBASE BASIC variable that has previously been opened to a file using the OPEN statement. If variable1 is not specified then the default file is assumed. The expression should evaluate to a valid record key for the file.
  • If no record is found and can be read from the file then it is mapped into the array and executes the THEN statements (if any). If the record cannot be read from the file then array is unchanged and executes the ELSE statements (if any).
  • If the record could not be read because another process already had a lock on the record then one of two actions is taken. If the LOCKED clause was specified in the statement then the statements dependent on it are executed. If no LOCKED clause was specified then the statement blocks (hangs)
  • until the other process releases the lock. If a LOCKED clause is used and the read is successful, a lock will be set.
  • If the SETTING clause is specified, setvar will be set to the number of fields in the record on a successful read. If the read fails, setvar will be set to one of these values.

If ON ERROR is specified, it executes the statements following the ON ERROR clause for any Incremental File Errors except 128.

The record is mapped into the array using a predefined algorithm. The record is expected to consist of a number of Field separated records, which are then assigned one at a time to each successive element of the matrix. If there were more fields in the record than elements in the array, then the final element of the array will be assigned all remaining fields. If there were fewer fields in the record than elements in the array then remaining array elements will be assigned a null value.

Note that if multi-values are read into an array element they will then be referenced individually as:

Array(n)<1,m>
1

not

Array(n)<m>
1

An example of use is as:

OPEN "test_rec" TO file_variable ELSE ABORT 201, "test_rec"
DIM sample_arr(10)

MATREAD sample_arr FROM file_variable , 002 SETTING result_variable ON ERROR
    CRT "Error number ":result_variable:" occurred, preventing record read."
    STOP
END THEN
    CRT 'Record read successfully'
    CRT "Number of attributes in record = ": result_variable
END ELSE
    CRT 'Record not on file'
END
1
2
3
4
5
6
7
8
9
10
11
12

Go back to jBASE BASIC

Go back to Programmers' Reference Guide