HP OpenVMS Systems Documentation

Content starts here

HP BASIC for OpenVMS
Reference Manual


Previous Contents Index


INSTR

The INSTR function searches for a substring within a string. It returns the position of the substring's starting character.

Format

int-var = INSTR (int-exp , str-exp1, str-exp2)


Syntax Rules

  • Int-exp specifies the character position in the main string at which HP BASIC starts the search.
  • Str-exp1 specifies the main string.
  • Str-exp2 specifies the substring.

Remarks

  • The INSTR function searches str-exp1, the main string, for the first occurrence of a substring, str-exp2, and returns the position of the substring's first character.
  • INSTR returns the character position in the main string at which HP BASIC finds the substring, except in the following situations:
    • If only the substring is null, and if int-exp is less than or equal to zero, INSTR returns a value of 1.
    • If only the substring is null, and if int-exp is equal to or greater than 1 and less than or equal to the length of the main string, INSTR returns the value of int-exp.
    • If only the substring is null, and if int-exp is greater than the length of the main string, INSTR returns the main string's length plus 1.
    • If the substring is not null, and if int-exp is greater than the length of the main string, INSTR returns a value of zero.
    • If only the main string is null, INSTR returns a value of zero.
    • If both the main string and the substring are null, INSTR returns a 1.
  • If HP BASIC cannot find the substring, INSTR returns a value of zero.
  • If int-exp does not equal 1, HP BASIC still counts from the beginning of the main string to calculate the starting position of the substring. That is, HP BASIC counts character positions starting at position 1, regardless of where you specify the start of the search. For example, if you specify 10 as the start of the search and HP BASIC finds the substring at position 15, INSTR returns the value 15.
  • If int-exp is less than 1, HP BASIC assumes a starting position of 1.
  • If you specify a floating-point expression for int-exp, HP BASIC truncates it to an integer of the default size.

Example


DECLARE STRING alpha,   &
        INTEGER result
alpha = "ABCDEF"
result = INSTR(1,alpha,"DEF")
PRINT result

Output


 4

INT

The INT function returns the floating-point value of the largest whole number less than or equal to a specified expression.

Format

real-var = INT (real-exp)


Syntax Rules

HP BASIC expects the argument of the INT function to be a real expression. When the argument is a real expression, HP BASIC returns a value of the same floating-point size. When the argument is not a real expression, HP BASIC converts the argument to the default floating-point size and returns a value of the default floating-point size.


Remarks

If real-exp is negative, HP BASIC returns the largest whole number less than or equal to real-exp. For example, INT(-5.3) is -6.


Examples

Example 1


DECLARE SINGLE any_num, result
any_num = 6.667
result = INT(any_num)
PRINT result

Output


 6

Example 2


!This example contrasts the INT and FIX functions
DECLARE SINGLE test_num
test_num = -32.7
PRINT "INT OF -32.7 IS: "; INT(test_num)
PRINT "FIX OF -32.7 IS: "; FIX(test_num)

Output


INT OF -32.7 IS: -33
FIX OF -32.7 IS: -32

INTEGER

The INTEGER function converts a numeric expression or numeric string to a specified or default INTEGER data type.

Format



Syntax Rules

Exp can be either numeric or string. A string expression can contain the ASCII digits 0 to 9, a plus sign (+), or a minus sign (--).


Remarks

  • HP BASIC evaluates exp, then converts it to the specified INTEGER size. If you do not specify a size, HP BASIC uses the default INTEGER size.
  • If exp is a string, HP BASIC ignores leading and trailing spaces and tabs.
  • The INTEGER function returns a value of zero when a string argument contains only spaces and tabs, or when it is null.
  • The INTEGER function truncates the decimal portion of REAL and DECIMAL numbers, or rounds if the /ROUND_DECIMAL qualifier is used.

Example


INPUT "Enter a floating-point number";F_P
PRINT INTEGER(F_P, WORD)

Output


Enter a floating-point number? 76.99
 76

ITERATE

The ITERATE statement allows you to explicitly reexecute a loop.

Format

ITERATE [ label ]


Syntax Rules

  • Label is the label of the first statement of a FOR...NEXT, WHILE, or UNTIL loop.
  • Label must conform to the rules for naming variables.

Remarks

  • ITERATE is equivalent to an unconditional branch to the current loop's NEXT statement. If you supply a label, ITERATE transfers control to the NEXT statement in the specified loop. If you do not supply a label, ITERATE transfers control to the current loop's NEXT statement.
  • The ITERATE statement can be used only within a FOR...NEXT, WHILE, or UNTIL loop.

Example


WHEN ERROR IN
Date_loop:  WHILE 1% = 1%
                  GET #1
                  ITERATE Date_loop IF Day$ <> Today$
                  ITERATE Date_loop IF Month$ <> This_month$
                  ITERATE Date_loop IF Year$ <> This_year$
                  PRINT Item$
            NEXT
USE
   IF ERR = 11
   THEN
            CONTINUE DONE
   ELSE
            EXIT HANDLER
   END IF
END WHEN
Done:  END

KILL

The KILL statement deletes a disk file, removes the file's directory entry, and releases the file's storage space.

Format

KILL file-spec


Syntax Rules

File-spec can be a quoted string constant, a string variable, or a string expression. It cannot be an unquoted string constant.


Remarks

  • The KILL statement marks a file for deletion but does not delete the file until all users have closed it.
  • If you do not specify a complete file specification, HP BASIC uses the default device and directory. If you do not specify a file version, HP BASIC deletes the highest version of the file.
  • The file must exist, or HP BASIC signals an error.
  • You can delete a file in another directory if you have access to that directory and privilege to delete the file.

Example


KILL "TEMP.DAT"

LBOUND

The LBOUND function returns the lower bounds of a compile-time or run-time dimensioned array.

Format

num-var = LBOUND (array-name [ , int-exp ])


Syntax Rules

  • Array-name must specify an array that has been either explicitly or implicitly declared.
  • Int-exp specifies the number of the dimension for which you have requested the lower bounds.

Remarks

  • If you do not specify a dimension, HP BASIC automatically returns the lower bounds of the first dimension.
  • If you specify a numeric expression that is less than or equal to zero, HP BASIC signals an error.
  • If you specify a numeric expression that exceeds the number of dimensions, HP BASIC signals an error.

Example


DECLARE INTEGER CONSTANT B = 5
DIM A(B)
account_num = 1
FOR dim_num = LBOUND (A) TO 5
    A(dim_num) = account_num
    account_num = account_num + 1
    PRINT A(dim_num)
NEXT dim_num

Output


1
2
3
4
5
6

LEFT$

The LEFT$ function extracts a specified substring from a string's left side, leaving the main string unchanged.

Format

str-var = LEFT[$] (str-exp, int-exp)


Syntax Rules

  • Int-exp specifies the number of characters to be extracted from the left side of str-exp.
  • If you specify a floating-point expression for int-exp, HP BASIC truncates it to an integer of the default size.

Remarks

  • The LEFT$ function extracts a substring from the left of the specified str-exp and stores it in str-var.
  • If int-exp is less than 1, LEFT$ returns a null string.
  • If int-exp is greater than the length of str-exp, LEFT$ returns the entire string.

Example


DECLARE STRING sub_string, main_string
main_string = "1234567"
sub_string = LEFT$(main_string, 4)
PRINT sub_string

Output


1234

LEN

The LEN function returns an integer value equal to the number of characters in a specified string.

Format

int-var = LEN (str-exp)


Syntax Rules

None


Remarks

  • If str-exp is null, LEN returns a value of zero.
  • The length of str-exp includes leading, trailing, and embedded blanks. Tabs in str-exp are treated as a single space.
  • The value returned by the LEN function is a LONG integer.

Example


DECLARE STRING alpha, &
        INTEGER length
alpha = "ABCDEFG"
length = LEN(alpha)
PRINT length

Output


 7

LET

The LET statement assigns a value to one or more variables.

Format

[LET] var,... = exp


Syntax Rules

  • Var cannot be a DEF or FUNCTION name unless the LET statement occurs inside that DEF block or in that FUNCTION subprogram.
  • The keyword LET is optional.

Remarks

  • You cannot assign string data to a numeric variable or unquoted numeric data to a string variable.
  • The value assigned to a numeric variable is converted to the variable's data type. For example, if you assign a floating-point value to an integer variable, HP BASIC truncates the value to an integer.
  • For dynamic strings, the destination string's length equals the source string's length.
  • When you assign a value to a fixed-length string variable (a variable declared in a COMMON, MAP, or RECORD statement), the value is left-justified and padded with spaces or truncated to match the length of the string variable.

Example


DECLARE STRING alpha, &
        INTEGER length
LET alpha = "ABCDEFG"
LET length = LEN(alpha)
PRINT length

Output


 7

LINPUT

The LINPUT statement assigns a string value, without line terminators, from a terminal or terminal-format file to a string variable.

Format



Syntax Rules

  • Chnl-exp is a numeric expression that specifies a channel number associated with a file. It must be immediately preceded by a number sign (#).
  • Str-var1 and str-var2 cannot be DEF function names unless the LINPUT statement is inside the multiline DEF that defines the function.
  • You can include more than one string constant in a LINPUT statement. Str-const1 is issued for str-var1, str-const2 for str-var2, and so on.
  • The separator (comma or semicolon) that directly follows str-var1 and str-var2 has no formatting effect. HP BASIC always advances to a new line when you terminate input with a carriage return.
  • The separator character that directly follows str-const1 and str-const2 determines where the question mark (if requested) is displayed and where the cursor is positioned for input.
    • A comma causes HP BASIC to skip to the next print zone to display the question mark unless a SET NO PROMPT statement has been executed. For example:


      DECLARE STRING your_name
      LINPUT "Name",your_name
      

      Output


      Name             ?
      
    • A semicolon causes HP BASIC to display the question mark next to str-const unless a SET NO PROMPT statement has been executed. For example:


      DECLARE STRING your_name
      LINPUT "What is your name";your_name
      

      Output


      What is your name?
      
  • HP BASIC always advances to a new line when you terminate input with a carriage return.

Remarks

  • The default chnl-exp is #0 (the controlling terminal). If you specify a channel, the file associated with that channel must have been opened with ACCESS READ or MODIFY.
  • HP BASIC signals an error if the LINPUT statement has no argument.
  • If input comes from a terminal, HP BASIC displays the contents of str-const1, if present. If the terminal is open on channel #0, HP BASIC also displays a question mark (?).
  • You can disable the question mark prompt by using the SET NO PROMPT statement. See the SET PROMPT statement for more information.
  • The LINPUT statement assigns all characters except any line terminators to str-var1 and str-var2. Single and double quotation marks, commas, tabs, leading and trailing spaces, or other special characters in the string are part of the data.
  • If the RETRY, CONTINUE, or RESUME statement transfers control to a LINPUT statement, the LINPUT statement retrieves a new record regardless of any data left in the previous record.
  • After a successful LINPUT statement, the RECOUNT variable contains the number of bytes transferred from the file or terminal to the record buffer.
  • If you terminate input text with Ctrl/Z, HP BASIC assigns the value to the variable and signals "End of file on device" (ERR=11) when the next terminal input statement executes.

Example


DECLARE STRING last_name
LINPUT "ENTER YOUR LAST NAME";Last_name
LINPUT #2%, Last_name

LOC

The LOC function returns a longword integer specifying the virtual address of a simple or subscripted variable, or the address of an external function. For dynamic strings, the LOC function returns the address of the descriptor rather than the address of the data.

Format



Syntax Rules

  • Var can be any local or external, simple or subscripted variable.
  • Var cannot be a virtual array element.
  • Ext-routine can be the name of an external function.

Remarks

  • The LOC function always returns a LONG value.
  • The LOC function is useful for passing the address of an external function as a parameter to a procedure. When passing a routine address as a parameter, you should usually pass the address by value. For example, OpenVMS system services expect to receive AST procedure entry masks by reference; therefore, the address of the entry mask should be in the argument list on the stack.

Example


DECLARE INTEGER A, B
A = 12
B = LOC(A)
PRINT B

Output


 2146799372

LOG

The LOG function returns the natural logarithm (base e) of a specified number. The LOG function is the inverse of the EXP function.

Format

real-var = LOG (real-exp)


Syntax Rules

None


Remarks

  • Real-exp must be greater than zero. An attempt to find the logarithm of zero or a negative number causes HP BASIC to signal "Illegal argument in LOG" (ERR=53).
  • The LOG function uses the mathematical constant e as a base. HP BASIC approximates e to be 2.71828182845905.
  • The LOG function returns the exponent to which e must be raised to equal real-exp.
  • HP BASIC expects the argument of the LOG function to be a real expression. When the argument is a real expression, HP BASIC returns a value of the same floating-point size. When the argument is not a real expression, HP BASIC converts the argument to the default floating-point size and returns a value of the default floating-point size.

Example


DECLARE SINGLE exponent
exponent = LOG(98.6)
PRINT exponent

Output


 4.59107

LOG10

The LOG10 function returns the common logarithm (base 10) of a specified number.

Format

real-var = LOG10 (real-exp)


Syntax Rules

None


Remarks

  • Real-exp must be larger than zero. An attempt to find the logarithm of zero or a negative number causes HP BASIC to signal "Illegal argument in LOG" (ERR=53).
  • The LOG10 function returns the exponent to which 10 must be raised to equal real-exp.
  • HP BASIC expects the argument of the LOG10 function to be a real expression. When the argument is a real expression, HP BASIC returns a value of the same floating-point size. When the argument is not a real expression, HP BASIC converts the argument to the default floating-point size and returns a value of the default floating-point size.

Example


DECLARE SINGLE exp_base_10
exp_base_10 = LOG10(250)
PRINT exp_base_10

Output


 2.39794


Previous Next Contents Index