4 Data Variables and Data handling

Assigning Values to Variables

Values can be assigned to variables either from within a program or from external sources (normally a keyboard or data file). This chapter describes most of the statements used to assign values; the others, used in file handling, are described in page 195 . The statements currently covered are as follows:

NOTE: The Keywords INPUT, LINPUT, EDIT, ENTER and LENTER are available on HP-UX systems, only.

LET

READ (from DATA)

INPUT

LINPUT

EDIT

ENTER

LENTER

The LET statement was introduced earlier. Many other statements also assign values to variables (READ#, ASSIGN, READ LABEL, etc.) as described in other chapters of the manual.

The READ and DATA Statements

To assign values to variables from within a program, the DATA statement is used with READ. The DATA statement provides values; READ specifies the variables for which values are to be obtained.

READ variable name1 [,variable name2 . . . ]

Text can be quoted or unquoted. For example:

70   DATA 88,April,"100","Pay=",95
80   READ A,Date$,Pay$[5,7],Pay$[1,4],Array(1)
The variables specified in the READ statement can be any variable type, including an array identifier which specifies an entire array. The subscripts can be any numeric expression except one containing a function subprogram (FN) reference. Array elements are read in order with the rightmost subscript varying fastest.

For example:

10   OPTION BASE 1
20   DIM A(2,2,2) ! A 3-dimensional 8 element array (2*2*2)
30   DATA 1,2,3,4,5,6,7,8
40   READ A(*)
50   PRINT A(*)
60   END
RUN
1                   2

3                   4

5                   6

7                   8
The array elements are assigned values in this order:

A(1,1,1)A(1,1,2)A(1,2,1)A(1,2,2)A(2,1,1)A(2,1,2)A(2,2,1)A(2,2,2)
READ is programmable only; it cannot be executed from the keyboard.

The DATA Pointer

The computer uses an internal mechanism called a DATA pointer to locate the next data item that is to be read. When the program is run, the Data pointer points to the first (leftmost) item of the first (lowest-numbered) DATA statement in the current segment. After this item is read the DATA pointer shifts one item to the right, pointing to the next item to be read. This operation is made each time a data item is read. After the last item in a DATA statement is read and another value is required by READ, the DATA pointer locates the next highest numbered DATA statement and is set to the first item in that statement. If there are no higher-numbered DATA statements, the data pointer remains at the end of the previous DATA statement; ERROR 36 indicates the end of data.

The location of the DATA statement within a program segment is unimportant. If there are multiple DATA statements, however, make sure they are in the order you want.

The RESTORE statement

The DATA pointer can be repositioned to the beginning of any DATA statement using the RESTORE statement:

RESTORE [line id]

If no line id is specified, the pointer is repositioned to the beginning of the lowest numbered DATA statement. If the specified line is not a DATA statement, then the first DATA statement following that line is accessed.

The next example shows that several READ statements can apply to the same DATA statement. It also shows that string values can be quoted or unquoted, though quotes are not part of the string. Notice that 7.31 is a string value assigned to A$.

100   READ A,B,C
110   READ D$,E
120   READ F$
130   DATA 4,5,6,7.31,2.69,"Hours"
The next example illustrates the use of RESTORE. The values in line 30 are assigned to five simple variables, then re-used as the values in array B.

10 OPTION BASE 1
20 DIM B(5)
30 DATA 4,9,16,25,30
40 FOR I=1 TO 5
50    READ C
60    DISP "Square root of";C;" is ";SQR(C)
70 NEXT I
80 DISP LIN(2)
90 RESTORE 30 ! Parameter not essential as only one DATA line here
100 READ B(*)
110 PRINT B(*)
120 END
RUN
Square root of 4  is 2
Square root of 9  is 3
Square root of 16  is 4
Square root of 25  is 5
Square root of 30  is 5.47722557505

4      9      16        25
30

The INPUT Statement

NOTE: The Keyword INPUT is available on HP-UX systems, only.

The INPUT statement suspends program execution, allowing values in the form of expressions to be assigned to variables from the keyboard. Syntax is as follows:

When the INPUT statement is executed, a ? or the prompt, if present, appears in the display line. The prompt may be any combination of characters normally used to tell the user what the request is. A value can then be input for each variable designated in the INPUT statement. For instance, the following statement requests two values (the prompt will be printed once):

340   INPUT "ENTER NAME AND EMPLOYEE NUMBER";Emp_name$,Emp_number
Values can be entered individually or in groups (separate each variable with a comma). Values for strings can be quoted or unquoted but an unquoted value may not contain a comma. For example, the values "A.Jones" and 250 can be assigned to the variables above in many ways; here are two:

A.Jones, 250 RETURN
or

"A.Jones" RETURN 5*50 RETURN
The ? reappears after RETURN is pressed until all values are input. If there is only one prompt, it will not reappear; ? appears instead. So it is best to use a prompt with each variable:

360   INPUT "ENTER NAME";Name$,"EMPLOYEE NUMBER";Number
Using a semicolon after the prompt causes the input to be entered on the same display line as the prompt, as in the previous examples. Using a comma after the prompt places the entry on the next display line.

Pressing RETURN without entering a value causes execution to continue with the next variable in the list. Variables not assigned values retain their previous value. For example:

40   X=5
50   PRINT X
60   INPUT "ENTER THREE VALUES:";A,B,X
By responding to the INPUT statement with 2,4, X retains its previously assigned value of 5.

The variable list can also include array identifiers:

370   INPUT A,B(*)
The INPUT statement is also used without parameters to suspend program execution. The operator can then enter data into the display, to be read by succeeding ENTER or LENTER statements (described later). Program execution is resumed by pressing RETURN.

The INPUT statement is programmable only; it cannot be executed from the keyboard.

The LINPUT Statement

NOTE: The Keyword LINPUT is available on HP-UX systems, only.

The LINPUT statement pauses program operation and allows the operator to enter an entire display line of information to a string variable. Pressing RETURN assigns the line to that string variable. Syntax for the LINPUT statement is as follows:

When LINPUT is executed, a ? or the prompt, if present, appears in the display line. Up to 160 characters can be entered with each LINPUT, although string subscripts could limit the input to less. For example:

380   LINPUT "ENTER HIS RESPONSE:",Response$[1,30]
The response could be: "Maximum 30 chars", David said

If a semicolon had followed the prompt above, the string value would appear immediately following the prompt. Pressing RETURN would then input the prompt along with the string. Pressing RETURN without typing in a value (not even a space) erases the current value of the string and sets it to the null string.

Notice that the LINPUT statement allows quotation marks to be input within a string variable; this is not possible with the INPUT statement.

The LINPUT statement cannot be executed from the keyboard and LINPUT cannot enter information from a protected display line (see section , Display Enhancements, on page 248).

The EDIT Statement

NOTE: The Keyword EDIT is available on HP-UX systems, only.

The current value of a string can be changed by using the EDIT statement. Syntax is as follows:

When the EDIT statement is executed, a ? or the prompt, if present, is displayed and followed by the current value of the specified string variable.

This value can be edited like any keyboard entry. You can clear the line, allowing a totally new value to be entered, as with LINPUT. Pressing RETURN stores the characters in the line as the value of the string. A trivial example is shown here:

100   DIM String$[60]
110   String$="Uncle Sam"
120   EDIT "CHANGE NAME?",String$
130   PRINT "CURRENT NAME IS:";String$
When line 120 is executed, CHANGE NAME is displayed. The string Uncle Sam then appears on the next line. The character editing keys may now be used to change the name. Pressing RETURN inputs the entire line into the variable. Line 130 prints the new name.

If a semicolon had followed the prompt in the EDIT statement, the string value would be displayed immediately following the prompt. Pressing RETURN would then input the prompt along with the string. Pressing RETURN without entering a value re-enters the current value of the string. Clearing the line before pressing RETURN, will set the variable to the null string.

The limit on the length of the string being edited is 160 characters, the display line length. This can be avoided by using substrings and multiple EDITs.

The EDIT statement cannot be executed from the keyboard.

The ENTER Statement

NOTE: The Keyword ENTER is available on HP-UX systems, only.

The ENTER statement reads data already on the display into the specified variables. It does not pause to allow keyboard entry. The syntax is as follows:

ENTER variable name1 [,variable name2 . . . ]

Data input begins at the current position of the display cursor and continues until the variable list is filled. As with INPUT, the variables read must satisfy the variable types in the list. If not, an error occurs.

The cursor position can be altered before executing ENTER by using the CURSOR statement (see section , Display Enhancements, on page 248). The ENTER statement cannot be executed from the keyboard.

The ENTER statement is intended for use with software which places forms on the display and specifies locations or input fields where the operator enters data (see the next sample form). After data is entered into the fields, ENTER and LENTER statements are used to read the data into variables according to a preset order.

                              New Customer Entry

Customer Name:

Surname:______________ First name:__________________  

Customer Address:

Street:___________________         

Town: ______________  State: _____________ 

Zip Code:________________

Telephone:

Area code:______  Number: _______
NOTE: Enter all the customer address information. Press TAB to move through the form, and press RETURN when complete.

The following program segment governs the above form. The INPUT statement on line 240 halts the program. Information on a new customer may then be entered. The TAB key is used to move from field to field. The RETURN key should be pressed upon completion of the form. Program execution then restarts and the ENTER statements read the data into the program variables in the preset sequence. Note that the Address lines, although separate entries on the screen, are loaded as substrings of one large string variable.


220 Input_data:!
230     CURSOR (5,5)   ! set cursor at first field.
240     INPUT          ! allow operator to fill fields.
250     CURSOR (5,5)   ! Reset cursor to first screen input
260     ENTER Surname$,Firstname$
270     ENTER Address$[1,19]
280     ENTER Address$[20,34],Address$[35,39]
290     ENTER Zip$,Phone$[1,9],Phone$[10,19]
These operations are described more fully in page 249 .

The LENTER Statement

NOTE: The Keyword LENTER is available on HP-UX systems, only.

The LENTER statement inputs data from one line of display into a specified variable, like LINPUT, but, like ENTER, does not pause to allow keyboard entry. Syntax is as follows:

LENTER string variable

The line length is limited to 512 characters, although string subscripts in LENTER could limit the input to less.

LENTER may only be used in a program, and it may not enter data from a protected display line (ERROR 38 is returned; protected display lines are fully covered in page 249 ). Use XLENTER to read a protected line.

The following program uses LENTER to load information provided by a CAT statement into a string array. The information can then be analyzed. (CAT does not store data; it merely outputs it to the SYSTEM PRINTER, here, of course, the display.)

The program prompts for the required volume and the file type which you wish to examine. The CAT (catalog) statement displays the file catalog for the requested directory. The Load_array routine then stores the catalog, line-by-line, into the string array Cat$. The CURSOR statement, line 50, positions the display cursor at the first line of the catalog. The Search routine finds and lists the files of the specified type.

The FOR loop in Load_array is performed 150 times (once for each array element), unless there are fewer than 150 files in the directory.

10 DIM Cat$(150)[80],Dir$[6],Type$[4]
20 DISP "~~" ! Alternate char set "CURSOR HOME, CLEAR DISPLAY"
30 INPUT "Enter Volume name: ";Dir$
40 INPUT "Now enter the File Type to be listed";Type$
50 CAT ","&Dir$  ! List required catalog
60 CURSOR (1,5) ! Set cursor to 1st char 4th line
70 ! note that the first 4 lines of the display must be skipped
80 !
90 Load_array:  ! Load CAT output into array Cat$
100    FOR Line=1 to 150 ! so don't over-run array boundary
110        LENTER Cat$(Line)
120    NEXT Line
130 !
140 Search:  ! Find and display files of specified type
150    DISP "~ ~" ! Alternate char set "CURSOR HOME, CLEAR DISPLAY"
160    DISP SPA(7);"FILE TYPE: ";Type$;SPA(5);" ON VOLUME: ";Dir$
170    FOR Line=1 TO 150
180        IF POS(Cat$(Line),"."&Type$ THEN DISP Cat$(Line)
200    NEXT Line
210 END
Here is the result of a sample run:

       FILE TYPE: PROG      ON VOLUME: LOCAL
-rw-rw-rw    1  john     users       580  Mar  2  1997 AK.PROG
-rw-rw-rw    1  john     users       182  Feb  8 16:35 BK.PROG
-rw-rw-rw    1  john     users       344  Mar  2  1997 INFO.PROG
-rw-rw-rw-   1  john     users       512  Mar  2  1997 KEYTST.PROG
-rw-rw-rw-   1  john     users       960  Feb 12 11:36 LENTER.PROG
-rw-rw-rw-   1  john     users      1504  Jan 28 17:40 akprog.PROG
A full description of the CURSOR statement is given in page 249 . CAT and the other Volume (HP-UX) operations are described in page 195 and page 25 .

The XLENTER Statement

NOTE: The Keyword XLENTER is available on HP-UX systems, only.

With extended LENTER up to 512 characters and protected lines may be ENTERed.

Syntax:

XLENTER string variable

Sample hardcopy routine using XLENTER:

SUB Hardcopy
   DIM A$[512]          ! Line buffer
   X=XPOS               ! Save current x/y position
   Y=YPOS
   PRINTER IS 0         ! Open printer
   FOR I=1 TO 21
      CURSOR(1,I)       ! Read and print screen line
      XLENTER A$
      PRINT A$
   NEXT I
   PRINTER IS 8         ! Close printer
   CURSOR (X,Y)         ! Restore cursor position
SUBEND
This routine will work with form and protected lines too.

The ACCEPT Statement

NOTE: The Keyword ACCEPT is available on HP-UX systems, only.

The ACCEPT statement loads a string into a string variable without displaying (echoing) the input. It is used for the entry of sensitive information such as passwords, where you do not wish the input to be visible on the screen. Syntax for the statement is as follows:

ACCEPT string variable

The ACCEPT statement has certain other characteristics:

Here is a sample of code using the ACCEPT statement. The operator has only one attempt at the password here; more often a loop is used to allow two or three tries.

10    PRINT "Please enter the Sales Ledger Password"
20    ACCEPT Pass$
30   IF Pass$<>"Hyacinth" THEN Inval_error ! Invalid password; no entry
40         ELSE Sales_ledger ! Password O.K so goto sales ledger
.
.
450 Inval_error: !
460     DISP "Invalid Sales ledger password - entry not permitted"
470     STOP

KBCODE

NOTE: The Keyword KBCODE is available on HP-UX systems, only.

Waits for a key and returns internal code of key.

This internal code is simply the ROMAN8 code ('A' = 65) for all non function keys. All function keys return codes of 256 + id (e.g. F1 = 265, F2 = 266as defined by curses.h).

In this sample program you could use normal keys as softkeys:

   DISP "Select option (1 .. 3 or E)"
   LOOP
      K = KBCODE
      EXIT IF CHR$(K)="E"
      SELECT CHR$(K)
      CASE "1","2","3":
         DISP "-> option ";K-NUM("0")
      CASE ELSE
         BEEP
      END SELECT
   END LOOP
   END
This is a more complex example. It could be used as a replacement of the ACCEPT statement. It will maintain an edit string of given length. For each character you enter a '*' as output. You can correct your input with the BACKSPACE key. Input will be finished either if you press RETURN or if you enter Max_len characters:

     DEF FNAccept$(Max_len)
        DIM A$[Max_len]
        LOOP
           C=KBCODE
           SELECT C
           CASE 13                             ! CR
              EXIT IF 1
           CASE 0 TO 7,9 TO 12,14 TO 31,>127   ! CONTROL
              BEEP
           CASE 8                              ! BACKSPACE
              IF LEN(A$) THEN
                 A$=A$[1,LEN(A$)-1]
                 DISP "~ ~";                   ! "BS SPACE BS"
              ELSE
                 BEEP
              END IF
           CASE ELSE                           ! CHARACTER
              A$=A$&CHR$(C)
              DISP "*";
              IF LEN(A$)=Max_len THEN
                 BEEP
                 EXIT IF 1
              END IF
           END SELECT
        END LOOP
        RETURN A$
     FNEND

Eloquence Language Manual - 19 DEC 2002