[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP BASIC for OpenVMS
|
Previous | Contents | Index |
A terminal line contains zones that are 14 character positions wide. The number of zones in a line depends on the width of your terminal: a 72-character line contains 5 zones, which start in columns 1, 15, 29, 43, and 57. A 132-character line has additional print zones starting at columns 71, 85, 99, and 113.
The PRINT statement formats program output into these zones in different ways, depending on the character that separates the elements to be printed. If a comma precedes the PRINT item, BASIC prints the item at the beginning of the next print zone. If the last print zone on a line is filled, BASIC continues output at the first print zone on the next line. For example:
INPUT A ,B ,C ,D ,E ,F PRINT A ,B ,C ,D ,E ,F END |
? 5,10,15,20,25,30[Return] 5 10 15 20 25 30 |
BASIC skips one print zone for each extra comma between list elements. For example, the following program prints the value of A in the first zone and the value of B in the third zone:
A = 5 B = 10 PRINT "first zone",,"third zone" PRINT A,,B END |
first zone third zone 5 10 |
If you separate print elements with a semicolon, BASIC does not move to the next print zone. In the following example, the first PRINT statement prints two numbers. (Printed numbers are preceded by a space or a minus sign and followed by one space.) The second PRINT statement prints two strings.
PRINT 10; 20 PRINT "ABC"; "XYZ" END |
10 20 ABCXYZ |
Whether you use a comma or a semicolon at the end of the PRINT statement, the cursor remains at its current position until BASIC encounters another PRINT or INPUT statement. In the following example, BASIC prints the current values of X, Y, and Z on one line because a comma follows the last item in the line PRINT X, Y:
INPUT X,Y,Z PRINT X,Y, PRINT Z END |
? 5,10,15 5 10 15 |
The following example shows PRINT statements using a comma, a semicolon, and no formatting character after the last print item:
!No comma after I%, so each element !Prints on its own line ! PRINT I% FOR I% = 1% TO 10% PRINT ! !A comma follows J%, so each !element prints in a separate zone ! MARGIN 80% PRINT J%, FOR J% = 1% TO 10% PRINT ! !A semicolon follows K%, so print !elements are packed together ! PRINT K%; FOR K% = 1% TO 10% END |
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 |
Commas and semicolons also let you control the placement of string output. For example:
PRINT "first zone",,"third zone",,"fifth zone" END |
first zone third zone fifth zone |
The extra comma between strings causes BASIC to skip another print zone. In the following example, the first string is longer than the print zone. When the two strings are printed, the second string begins in the third print zone because that is the next available print zone after the first string is printed.
PRINT "abcdefghijklmnopqrstuvwxyz","pizza" PRINT "first zone","second zone","third zone" |
abcdefghijklmnopqrstuvwxyz pizza first zone second zone third zone |
BASIC prints strings exactly as you type them, with no leading or trailing spaces. It does not print quotation marks unless they are delimited by another matching pair. For example:
PRINT 'PRINTING "QUOTATION" MARKS' END |
PRINTING "QUOTATION" MARKS |
BASIC follows these rules for printing numbers:
HP BASIC follows these rules for printing floating-point numbers:
The PRINT statement displays only up to 6 digits of precision for floating-point numbers. This corresponds to the precision of the SINGLE or SFLOAT data types. To display the extra digits in DOUBLE, GFLOAT, TFLOAT, or XFLOAT numbers, you must use the PRINT USING statement. See Chapter 14 for more information about the PRINT USING statement.
The following example shows how BASIC prints various numbers with single precision:
FOR I = 1 TO 20 PRINT 2^(-I),I,2^I NEXT I END |
.5 1 2 .25 2 4 .125 3 8 .0625 4 16 .03125 5 32 .015625 6 64 .78125E-02 7 128 .390625E-02 8 256 .195313E-02 9 512 .976563E-03 10 1024 .488281E-03 11 2048 .244141E-03 12 4096 .12207E-03 13 8192 .610352E-04 14 16384 .305176E-04 15 32768 .152588E-04 16 65536 .767939E-05 17 131072 .38147E-05 18 262144 .190735E-05 19 524288 .953674E-06 20 .104858E+07 |
Terminal-format files let you perform simple I/O to disk files. The records in a terminal-format file must be accessed sequentially. That is, you must access the records in the file one by one, from the first to the last. You can add new records only at the end of the file.
Just as the INPUT, LINPUT, and INPUT LINE statements receive information from a terminal, the INPUT #, LINPUT #, and INPUT LINE # statements receive information from a terminal-format file. And, as the PRINT statement sends information to the terminal, the PRINT # statement sends information to a terminal-format file.
Terminal-format files are useful for creating files to be printed on a line printer, or for supplying a program with moderate amounts of input. However, if you want to use the same file for both input and output, you should not use terminal-format files. Instead, use sequential, relative, or indexed files. For more information, see Chapter 13.
You do not have to use a program to create a terminal-format file. You
can use a text editor to create a file and insert data, then use a
BASIC program to open the file and retrieve the data.
5.3.1 Opening and Closing a Terminal-Format File
You use the OPEN statement to create a file, or to gain access to an existing file. If you do not specify either FOR INPUT or FOR OUTPUT in the OPEN statement, BASIC tries to open an existing file. If the file does not exist, BASIC creates a new one.
The channel specification lets you associate a number with the file for as long as the file is open. All I/O operations to or from the file use this number.
When you are finished accessing a file, you close it with the CLOSE
statement.
5.3.2 Writing Records to a Terminal-Format File
The following example receives information from a terminal, then writes the information to a terminal-format file as a report:
PRINT "This program creates a daily sales report file named SALES.DAT" OPEN "SALES.DAT" FOR OUTPUT AS FILE #4% PRINT #4%, "Salesperson","Sales Area","Items Sold" PRINT #4% INPUT "How many salespersons for today's report"; sales_persons% FOR I% = 1% TO sales_persons% INPUT "Salesperson's name"; s_name$ INPUT "Sales area"; area$ INPUT "Number of items sold"; items_sold% PRINT #4%, s_name$, area$, items_sold% NEXT I% CLOSE #4% END |
This program creates a daily sales report file named SALES.DAT How many salespersons for today's report? 3 Salesperson's name? JONES Sales area? NJ Items sold? 5 Salesperson's name? SMITH Sales area? NH Items sold? 6 Salesperson's name? BAINES Sales area? VT Items sold? 8 |
This program first prints a header explaining its purpose, then opens a terminal-format file on channel 4. After this file is opened, the two PRINT # statements place an explanatory header followed by a blank line into the file.
The program then prompts you for the number of salespersons for which data is to be entered. The FOR...NEXT loop prompts for the name, sales area, and items sold for each salesperson. The FOR...NEXT loop executes only as many times as there are salespersons. See Chapter 9 for more information about FOR...NEXT loops.
After the data has been entered for each salesperson, the program writes this information to the terminal-format file. Because the response to the first question was 3, the FOR...NEXT loop executes three times.
After the last item has been printed to the file, the program closes the file and ends. When you display the file with the DCL command TYPE, you see that the information is printed under the proper headers. You can also print the file on a line printer. The PRINT # statement formats the output in print zones as the PRINT statement does.
$ TYPE SALES.DAT Salesperson Sales Area Items Sold JONES NJ 5 SMITH NH 6 BAINES VT 8 |
An array is a set of data that is ordered in any number of dimensions.
This chapter describes how to create and use HP BASIC arrays.
6.1 Overview of Arrays
A one-dimensional array is called a list or vector. A two-dimensional array is called a matrix. HP BASIC arrays can have up to 32 dimensions, and a specific type of HP BASIC arrays can be redimensioned at run time. In addition, you can specify the data type of the values in an array by using data type keywords or suffixes.
The subscript of an element in an array defines that element's position in the array. When you create an array, you specify:
BASIC arrays are zero-based by default; that is, when calculating the number of elements in a dimension, you count from zero to the number of elements specified. For example, an array with an upper bound of 10 and no specified lower bound has 11 elements: 0 to 10, inclusive. The array My_array(3,3) has 16 elements: 0 to 3 in each dimension, or 42.
BASIC also lets you specify a lower bound for any or all dimensions in an array, unless the array is a virtual array. By specifying lower and upper bounds for arrays, you can make your array subscripts meaningful. For example, the following array contains sales information for the years 1990 to 1999:
DECLARE REAL Sales_data(1990 TO 1999) |
To refer to an element in the array Sales_data, you need only specify the year you are interested in. For example, to print the information for the year 1999, you would enter:
PRINT Sales_data(1999) |
You can create arrays either implicitly or explicitly. You implicitly create arrays having any number of dimensions by referencing an element of the array. If you implicitly create an array, BASIC sets the upper bound to 10 and the lower bound to zero. Therefore, any array that you create implicitly contains 11 elements in each dimension.
The following example refers to the array Student_grades. If the array has not been previously declared, BASIC will create a one-dimensional array with that name. The array contains 11 elements.
Student_grades(8) = "B" |
You create arrays explicitly by declaring them in a DIM, DECLARE, COMMON, or MAP statement, or record declaration. Note that if you want to specify lower bounds for your array subscripts, you must declare the array explicitly.
When you declare an array explicitly, the value that you give for the upper bound determines the maximum subscript value in that dimension. If you specify a lower bound, then that is the minimum subscript value in that dimension. If you do not specify a lower bound, BASIC sets the lower bound in that dimension to zero. You can specify bounds as either positive or negative values. However, the lower bound of each dimension must always be less than or equal to the upper bound for that dimension.
You can use MAT statements to create and manipulate arrays; however,
MAT statements are valid only on arrays of one or two dimensions. In
addition, the lower bounds of all dimensions in an array referenced in
a MAT statement must be zero.
6.2 Creating Arrays Explicitly
You can create arrays explicitly with four BASIC statements: DECLARE, DIMENSION, COMMON, and MAP.
In addition, you can declare arrays as components of a record data type. See Chapter 8 for more information about records.
Normally, you use the DECLARE statement to create arrays. However, you might want to create the array with another BASIC statement as follows:
When you create an array, the bounds you specify determine the array's size. The maximum value allowed for a bound can be as large as 2147483467; however, this number is actually limited by the amount of virtual storage available to you. Very large arrays and arrays with many dimensions can cause fatal errors at both compile time and run time.
The following restrictions apply to arrays:
The DECLARE statement creates and names variables and arrays. All elements of arrays created with the DECLARE statement are initialized to zero or the null string. The following statement creates a longword integer array with 11 elements:
DECLARE LONG FIRST_ARRAY(1980 TO 1990) |
Note that the STRING data type with the DECLARE statement causes the
creation of an array of dynamic strings. To create an array of
fixed-length strings, declare the array in a COMMON or MAP statement or
as part of a RECORD structure.
6.2.2 Creating Arrays with the DIM Statement
The DIM statement creates and names one or more arrays. Use the DIM statement to create an array when you want to:
When creating arrays with the DIM statement, you specify the data type of the array elements with a data type keyword, a special suffix on the array name, or both. The array name can be any valid variable name. If you do not supply a data type keyword, the data type is determined by the suffix of the array name:
Even if the DIM statement contains a data type keyword, the array name can still end in the appropriate data type suffix. This makes the data type of the array immediately obvious.
The DIM statement can be either executable or declarative. If the specified bounds are constants, the DIM statement is declarative. This means that the storage is allocated at compile time, and the array cannot appear in any other DIM statement.
However, if any of the specified bounds are variables (simple or subscripted), the DIM statement is executable. This means that the storage for the array is allocated at run time, and the array can be redimensioned with a DIM statement any number of times.
In the DIM statement, bounds can be either constants or variables (simple or subscripted), but not expressions. |
When an array is redimensioned with the executable DIM statement, the array can become larger or smaller than it was. However, redimensioning an array in this way causes it to be reinitialized, and all data in the array is lost.
In contrast, MAT statements let you redimension an array to be the same
size or smaller than it was. However, MAT statements redimension arrays
only when assigning values or performing matrix I/O; therefore, the
fact that MAT statements reinitialize the array does not matter. See
Section 6.6 for more information about MAT statements.
6.2.2.1 Declarative DIM Statements
Declarative DIM statements have integer constants as bounds. The percent sign is optional for bounds; however, BASIC signals the error "Integer constant required" if a constant bound contains a decimal point. The following statement creates a 101-element virtual array containing string data. The elements of this array can each have a maximum length of 256 characters.
DIM #1%, STRING VIRT_ARRAY(100) = 256% |
The following restrictions apply to the use of declarative DIM statements:
Previous | Next | Contents | Index |