File Management Overview | Statement List


Edit

This section covers the GRIP statements used to edit files in one of the scratch areas.

Many of the statements access, read, or write to and from text files. When you are working with these files, the structure of the data can be very important. The file is considered unformatted or formatted.

In an unformatted file, the data is listed in sequence with the fields separated only by a delimiter, such as a comma. For example:

10 ABCDE,FGHIJ,KLMNO

20 PQRST,UVWXY,Z1234

30 56789,ABCDE,FGHIJ

40 KLMNO,PQRST,UVWXY

In a formatted file, the data is spaced into columns and may be sectioned into groups of lines. For example:

10 ABCDE FGHIJ KLMNO

20 PQRST UVWXY Z1234

30

40 56789 ABCDE FGHIJ

50 KLMNO PQRST UVWXY

60

Unformatted INPUT using the WRITE statement causes a new line in the file to be created for each WRITE statement. If the data in the statement is in an array, the current delimiter will be placed between each element of the array.

For example, writing the coordinates of a point which are in an array COORDS(3) would result in the following line:

10 1.025,2.375,1.202

The next write would create a new line:

10 1.025,2.375,1.202

20 2.277,3.625,3.399

If you were to write the two points in a single write statement, the data would be placed on a single line:

10 1.025,2.375,1.202,2.277,3.625,3.399

Unformatted OUTPUT using the READ statement causes each field (separated by the delimiter) to be placed into each position of a specified array. You should be careful, if you are reading lines with multiple fields, to specify an array in your program.

Formatted INPUT/OUTPUT is accomplished by including the minor word USING followed by the necessary image strings. Image strings are string literals consisting of image fields which control the data format and location. The image field begins with a # character followed by any number of @ characters and a maximum of one embedded decimal point. In addition to specifying the beginning of an image field, the # character also serves as a character position for the output value. In image strings which consist of multiple image fields the fields may be separated by any combination of blank characters and/or text. However, separators are not required.

A one to one correspondence does not exist between the number of variables in a statement and the number of image fields. Therefore, if the number of variables exceed the number of image fields, the image string will be used repeatedly until the variable list has been exhausted. On the other hand, if the number of image fields exceed the number of variables, the remaining fields will be ignored.

Example

In this program there is only one image field and seven variables, therefore, the same image field would be used for each of the variables.

Declarations

NUMBER/WKDAY(7)

DATA/WKDAY,1,2,3,4,5,6,7

Print Statements

PRINT/USING,'D - # ',WKDAY

In the next program, there are eight image fields and seven variables, therefore, the last image field will be ignored.

Declarations

NUMBER/WKDAY(7)

DATA/WKDAY,1,2,3,4,5,6,7

Print Statement

PRINT/USING,$
'D - # D - # D - # D - # D - # D - # D - # D - #',$
WKDAY

An effective method of performing formatted reads and writes is to use a string variable as the image string. This approach is especially efficient when the same format is to be used in several input/output statements.

Declarations

NUMBER/WKDAY(7)

STRING/FMT(5)
DATA/WKDAY,1,2,3,4,5,6,7
DATA/FMT,'D-# '

Print Statement

PRINT/USING,FMT,WKDAY

The output shown below would be the same for all three of the examples above.

D-1 D-2 D-3 D-4 D-5 D-6 D-7

If a decimal point is used in an image field to output a numerical value, the decimal point in the image field will be aligned with the decimal point of the numerical value. Subsequently, the number of # and @ characters to the left of the decimal point must be equal to or greater than the number of digits in the integer portion of the value. If less characters than digits exist, asterisks will be output in the image field format instead of the numerical value. If more characters than digits exist, the remaining integer places will be output as blanks.

Conversely, the number of @ characters to the right of the decimal point does not have to be equal to the number of places past the decimal point in the numerical value. If less characters than digits exist, the output value will be rounded up to the next highest decimal place. If more characters than digits exist, the remaining decimal places will be output as zeros.

VALUE=54321.12345
PRINT/USING,'#@@@.@@@@@',VALUE
PRINT/USING,'#@@@@@.@@@@@',VALUE
PRINT/USING,'#@@@@.@@@@',VALUE
PRINT/USING,'#@@@@.@@@@@@@',VALUE

The program above would produce the following output.

****.*****
54321.12345
54321.1235
54321.1234500

In the first print statement above, the number of characters to the left of the decimal point do not accommodate the integer portion of the value, therefore, only asterisks are output.

In the second print statement, the number of characters to the left of the decimal point is one greater than the integer portion of the value, therefore, the first output position is left blank.

In the third print statement, the number of characters to the right of the decimal point do not accommodate the fractional portion of the value, therefore, the output value is truncated to the fourth decimal place. In addition, since the fifth decimal place of the numerical value is greater than or equal to five the fourth and last output decimal place is increased in value by one.

In the fourth print statement, the number of characters to the right of the decimal point is two greater than the fractional portion of the value, therefore, the last two output positions are zero.

If an image field containing a decimal point is used to output a string value, the decimal point will be treated as just another character position. In addition, character data is left justified in the image field, therefore, the first character in the data string will be output at the position of the # character.

If the data string is longer than the image field, the output data string will be truncated to the length of the image field. If the image field is longer than the data string, the remaining positions in the image field will be left blank.

The following is an example of the length of an image field.

Declarations

STRING/VALUE(26)

VALUE='ABCDEFGHIJKLMNOPQRTSUVWXYZ'
PRINT/USING,'#@@@@@@@@@@@@',VALUE
VALUE='ABCDEFGHIJKLM'
PRINT/USING,'#@@@@@@@@@@@@@@@@@@@@@@@@@',VALUE
PRINT/USING,'#@@@@@@.@@@@@',VALUE

Each of the PRINT statements above would output the letters A thru M; in the first print statement because the image field is shorter than the string literal, in the second statement because the string literal is shorter then the image field and in the third statement because the decimal is used as just another character position for string data.