BASIC09 Command Reference l 11

TYPE Defines a data type

Syntax: TYPE name = typedeclar [; typedeclar[;...]]

Function: Defines new data types (complex data structures). New data types are vectors (one-dimensional arrays) of previously defined types. Structures created by TYPE differ from arrays in that they can consist of elements of different types, and BASIC09 accesses elements by field names rather than by an indexed position.


Parameters:

name The name you select for the new data type.
typedecrar One or more type declarations, which can con
sist of field names, type declarations, and sub
scripts. Separate different types or different
lengths of string declarations with semicolons.

Notes:

0 Complex data structures allow you to create data types that are appropriate for a specific task. You can organize, read, and write associated data naturally. Also, BASIC09 establishes and defines element positions at compilation time. This saves time and overhead at run time because BASIC09 can access the elements of a data structure faster than it can access the elements of an array.


When you define new data structures using TYPE, you can can include any of the five existing data types (string, real, integer, byte, and Boolean), or you can include data structure types that you previously defined with TYPE. This means that your structures can be simple or very complex, such as non-rectangular data lists or trees.


TYPE does not create storage. You create storage using the DIM statement, after using TYPE.


0 To access elements of a data structure, use the field name as well as any appropriate element index.

11-177
BASIC09 Reference

0 For more information on creating and using complex data types, see "Complex Data Types" in Chapter 6.

Examples:

TYPE LIBRARY=TITLE,AUTHOR,PUBLISHER:STRINGL25J;
REFERENCE: INTEGER
DIM BOOKC500):LIBRARY

TYPE PARTS=ITEM,LOCATION:STRINGL201; CAT:REAL;
QUANTITY;INTEGER
DIM INVENTORYC1000):PARTS

Sample Program:

This procedure builds an array to contain a book reference list, including the book title, the author's name, the publisher, and a reference number. It does so by using TYPE to create a special data structure to store all the information for each book.

PROCEDURE books
pTYPE LIBRARY=TITLE,AUTHOR,PUBLISHER:STRINGL30l;
REFERENCE: INTEGER
DIM BOOKSC100):LIBRARY
OT = 0
pL00P
OT=T+ 1
INPUT "BOOK TITLE...",BT$ OBOOKSCT).TITLE=BT$ DEXITIF BOOKSCT).TITLE="" THEN pGOTO 1 0 0 DENDEXIT

OINPUT "Book Author... OBOOKSCT).AUTHOR=BA$

OINPUT "Book Publisher...",BP$ OBOOKSCT).PUBLISHER=BP$

OINPUT "Reference Number... DENDLOOP 1 000FOR X=1 TO T-1

OPRINT BOOKSCX).TITLE;

go ;

OHOOKSCX).PUHLISHER; "
ONEXT X
[]END

",HA$

,HOOKSCT).REFERENCE

"; BOOKSCX).AUTHOR;

"· HOOKSCX).REFERENCE

11-178




Syntax: REPEAT



Function: Ends a REPEAT loop. REPEAT establishes a loop that executes the encompassed procedure lines until the result of the expression following UNTIL is true. Because the loop is tested at the bottom, the lines within the loop are executed at least once.

Parameters:
procedures Statements you want to execute in the loop.
lines
expression A Boolean expression (the result must be
either True or False).
Examples:
REPEAT
COUNT = COUNT+1
UNTIL COUNT > 1 0 0

INPUT X,Y REPEAT X = X-1 Y = Y-1 UNTIL X<1 OR Y<0

See REPEAT for more information.


BASIC09 Reference

USING Formats PRINT output

Syntax: PRINT [#pa th] USING [format,] da to[;data...]

Function: Prints data using a format you specify. This statement is especially useful for printing report headings, accounting reports, checks, or any document requiring a specific format.

USING is actually an extension of the PRINT statement. The same rules that apply to the PRINT statement also apply to the PRINT USING statement (see PRINT).

Parameters:

path The number to an opened device or file. If you
do not specify path the default is #1, the video
screen (standard output device). To print to
another device or file, first OPEN a path to
that file or device (see OPEN).
format An expression specifying the arrangement of
the displayed data.
data Any numeric or string constant or variable.
Always enclose string constants within quota
tion marks. Separate all data items with
semicolons or commas.

See PRINT USING for more information.

11-180
BASIC09 Command Reference l 11

VAL Converts string data to numeric data

Syntax: vAL(string)

Function: Converts string-type data to numeric-type. VAL is the inverse of the STR$ function. It returns the real value represented by the characters in a string. If any character in the specified string is not numeric, BASIC09 returns an error.


Parameters:



Examples:

PRINT VAL(123)

A$="44.66"
PRINT VALCA$)

Sample Program:

This procedure calculates an exponential value, then adds the necessary number of zeroes to convert it to standard notation. It uses STR$ to convert the original number to a string, then uses VAL to convert the exponent into a value to determine the correct decimal place.


PROCEDURE bignum
pDIM C,PLACES,H,SIGN:STRING; EX,COUNT,NEWCOUNT,
DECIMAL: INTEGER

ODIM NEW,ZERO,NEWEST:STRINGL1001 pCOUNT=-1

pZERO="000000000000000000000000000000000000" ANEW="" NEWEST=""

C]INPUT "What number do you want to raise to the power of 14?...",NUM

DA=NUM^14
DH=STR$CA)
DEX=SUHSTRC"E",H)
pS IGN=MID$CH, EX+1 , 1
BASIC09 Reference

OPLACES=RIGHT$CH,LENCH)-EX-1)
pFOR T=1 TO LENCH)
OC=MID$CH,T,1 )
OIF C="." THEN
ODECIMAL=0
OGOTO 1 0
pEND I F
ODECIMAL=DECIMAL+1
DI F C="E" THEN 1 00
ONEW=NEW+C
1 00NEXT T
100ONEWCOUNT=VALCPLACES)-DECIMAL
ONEW=NEW+LEFT$CZERO,NEWCOUNT+1)
OFOR T=LENCNEW) TO 1 STEP -1
OCOUNT=COUNT+1
ONEWEST=MID$CNEW,T,1)+NEWEST
OIF MODCCOUNT,3)=2 AND T>1 THEN
ONEWEST=","+NEWEST
REND I F
ONEXT T
OPRINT NUM; " to the power of 14 = "; A
OPRINT "_ "; NEWEST
REND

11-182


WHILE/DO/ENDWHILE Establishes


Syntax: WHILE expression DO
        procedure lines

        ENDWHILE


Function: Establishes a loop that executes the encompassed procedure lines while the result of the expression following WHILE is true. Because the loop is tested at the top, the lines within the loop are never executed unless the expression is true.

Parameters:

expression A Boolean expression (has a result of True or
False).
procedure Program lines to execute if the expression is
lines true.

Examples:

WHILE COUNT < 12 DO COUNT = COUNT+1 ENDWHILE

Sample Program:

You must create a file of directory names using the GET sample program before you can use the following procedure. Copyutil uses the filenames created by the GET sample program to copy a directoty's files to another directory you specify. You must specify a directory name that does not exist. Copyutil uses a WHILE/DO/ENDWHILE loop to continue copying until BASIC09 reaches the end of the file.

11-183
BASIC09 Reference

PROCEDURE copyutil
ODIM PATH,T,COUNT:INTEGER; FILE,JOH,DIRNAME:STRING
OOPEN #PATH,"dirfile":READ
pINPUT "Name of new directory...",DIRNAME
OSHELL "MAKDIR "+DIRNAME
pSHELL "LOAD COPY"
AWHILE NOTCEOFC*PATH)) DO
DREAD #PATH,FILE
pJOH=FILE+" "+DIRNAME+"/"+FILE
DON ERROR GOTO 10
pPRINT "COPY "; JOB
OSHELL "COPY "+JOB
100ON ERROR
OENDWHILE
OCLOSE #PATH
DEN D

11-184
                BASIC09 Command Reference / 11


          WRITE Writes data to a sequential file or device


          Syntax: WRITE [#Path,] data


Function: Writes an ASCII record to a sequential file or to a device.

Parameters:

path A variable containing the path number of the
file or device to which you want to send data.
Path can be one of the the standard I/O paths
(0, 1, 2).
data The data you want to send over the specified
path.
Notes:

The following information deals with writing sequential disk files:

0 To write file records, you must first dimension a variable to
contain the path number of the file, then use OPEN or
CREATE to open a file in the WRITE or UPDATE access
mode.
Records can be of any length within a file.

    Individual data items in the input record are separated by ASCII null characters. You can also separate numeric items with comma or space character delimiters. Each input record is terminated by a carriage return character.


Examples:

WRITE #PATH,DATA$

WRITE #1,RESPONSE$

WRITE #OUTPUT,INDEXCX)

                                11-185

BASIC09 Reference

OPEN #PATH,"namefile":WRITE
FOR T=1 TO 10
READ NAME$
WRITE #PATH, NAME$
NEXT T
CLOSE #PATH
DATA "JIM", "JOE","SUE","TINA","WENDY"
DATA "SALL", "MICKIE","FRED","MARV","WINNIE"

Sample Program:

This procedure selects 100 random values between 1 and 10. It uses WRITE to place the values into a disk file. Next, it reads the values from the file and uses asterisks to indicate how many times RND selected each value.

PROCEDURE randlist
pDIM SHOW,HUCKET:STRING
pDIM T,PATH,SELECTC10),R:INTEGER
OHUCKET="************************'I
OFOR T=1 TO 1 0
pSELECTCT)=0
ONE XT T
pON ERROR GOTO 10
pSHELL "DEL RANDFILE"
100 ON ERROR
pCREATE *PATH,"randfile":UPDATE
OFOR T=1 TO 1 0 0
pR=RNDC9)+1
pWRITE *PATH,R
pNEXT T
pPRINT "Random Distribution"
OSEEK #PATH,0
pFOR T=1 TO 100
pREAD #PATH,NUM
OSELECTCNUM)=SELECTCNUM)+1
ONEXT T _
pFOR T=1 TO 1 0
OSHOW=RIGHT$CHUCKET,SELECTCT))
OPRINT USING "S6<,I3<,S2<,S20<","Number",T,":".
SHOW
ONEXT T
OCLOSE #PATH
pEND

11-186
        BASIC09 Command Reference / 11


XOR Returns the exclusive OR of two values Syntax: operandl XOR operand2

Function: Performs the logical exclusive OR operation on two

or more values, returning a value of either TRUE or FALSE.


Parameters:

operandl Boolean values or expressions (that result in
operand2 values of True or False).

Examples:

PRINT 02 XOR H>3

PRINT A$="YES" XOR H$="YES"

A
    Sample Program:


    This procedure lets two people type numbers until one of them guesses the number that the computer picks. It uses XOR to determine that one of the numbers typed is the correct number, but not both.


      PROCEDURE draw5traw

      pDIM NUM1,NUM2,R:INTEGER; A:BOOLEAN

      OPRINT "This program will help you pick"

      OPRINT "between two people. Choose who will be"

      OPRINT "Person 1 and who will be Person 2."

      OPRINT "Then, enter numbers between 1 and 10"

      OPRINT "when requested."

      pPRINT

      OR=RND(1 0

      1 00I NPUT "Per Son 1 , type a number and pre55

      ENTER...",NUM1

      OINPUT "Person 2, type a number and pre55

      ENTER...",NUM2

      DA=NUM1 =R XOR NUM2=R

      EIF A=FALSE THEN

      OPRINT "You'll have to try again..."

      OPRINT


11-187
BASIC09 Reference

pGOTO 1 0
OENDI F
pI F NUM1 =R THEN
pPRINT "You win, Person 1"
EIEND I F
OIF NUM2=R THEN
pPRINT "You win, Person 2"
DENDIF
pPRINT "The Number was..."; R
OEND

11-188