Variables

From VSI OpenVMS Wiki
Revision as of 09:28, 22 October 2018 by Darya.zelenina (talk | contribs) (Created page with "A '''variable''' is a symbolic name associated with a set of data such as a number or a string. In DCL, variables are implemented in symbols. =Data types= In...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A variable is a symbolic name associated with a set of data such as a number or a string. In DCL, variables are implemented in symbols.

Data types

In DCL, you can store numbers and strings as variables. Boolean values (TRUE and FALSE) can be stored as numbers (even or odd) or strings. Date and time values can be stored as strings and managed with lexical functions such as F$CVTIME() and F$TIME(). Arrays can be implemented with delimited strings.

Number

Variables assigned numeric values are stored as signed decimal numbers. Variables assigned the value 0 or 1 can act as boolean values in IF-THEN-ELSE structures:

$ a = 1
$ if a then write sys$output "Yes!"
Yes! 
 

String

String values can be assigned in a few ways depending on whether the value is case sensitive and whether it contains quotation marks as a character. Consider the following cases:

Case sensitive string

$ a = "Hello"
$ show sym a
  A = "Hello"

$ a := Hello
$ show sym a
  A = "HELLO"
 

Quotation marks

If you include another set of quotation marks inside of a string, the part inside of the inner quotes is going to be normalized, irrespective of whether the symbol is initialized with "=" or with ":=".

$ a = "These "quotes" are great"
$ write sys$output a
These QUOTES are great

$ a := "These "quotes" are great"
$ write sys$output a
These QUOTES are great
 

If you use a double set of quotes (or actually, any even number of quotes greater than zero) on the outer string, the assignment will result in an error:

$ a = ""These "quotes" are great""
%DCL-W-UNDSYM, undefined symbol - check validity and spelling
 \ARE\
 

Using a triple set of quotes on the outer string results in quotes being put on the outside of the string, and the inner quoted string normalized:

$ a = """These "quotes" are great"""
$ write sys$output a
"These QUOTES are great"
 

Adding two more sets of quotes on the outer string above 3 results in another one set of quotes being put on the outside of the string: 5 sets of quotes produce 2, 7 produce 3, 9 produce 4, and so on. The inner quotes are not preserved, and the quoted string is normalized:

$ a = """""These "quotes" are great"""""
$ write sys$output a
""These QUOTES are great""
 

To preserve quotes INSIDE of the string, use double quotes:

$ a = "These ""quotes"" are great"
$ write sys$output a
These "quotes" are great
 

Use quadruple quotes for double quotes inside of the string. Triple quotes result in the inner quoted string being normalized:

$ a = "These """quotes""" are great"
$ write sys$output a
These "QUOTES" are great
 

Another way is to use F$FAO() and put the substitution string in triple quotes:

$ a = f$fao("These !AS are great","""quotes""")
$ write sys$output a
These "quotes" are great
 

Symbol substitution inside of the string

You can use two leading single quotes and one trailing single quote to force symbol substitution inside of a quoted string:

$ name = "John"
$ a = "Hello ''name'"
$ write sys$output a
Hello John
 

However, consider using string concatenation and multiple arguments to the WRITE command whenever possible since that executes faster and uses less memory.

Booleans

The following values are considered TRUE:

  • odd integers
  • character strings represented odd integers ("13")
  • character strings beginning with t, T, y, or Y

Even integers and any other strings are considered FALSE.

Arrays

There is no separate data type for arrays in DCL, but you can implement them with strings. Here is how it works:

$ months = "Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec"
 

To access the fourth element of this array, use:

$ fourth = f$element(3,"/",months)
$ show sym fourth
  FOURTH = "Apr"
 

Remember that F$ELEMENT() starts the count of elements at 0.

If the element is out of range, its value will be the delimiter:

$ oops = f$element(14,"/",months)
$ show sym oops
  OOPS = "/"
 

You can loop over the elements of the array as follows:

 
$ months = "Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec"
$ count = 0
$loop:
$ i = f$element(count,"/",months)
$ if i .eqs. "/" then exit
$ write sys$output i
$ count = count + 1
$ goto loop
 

This produces the following output:

 
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
 

You can also implement multidimensional arrays or dictionaries in DCL by using different delimiters. However, note that strings in DCL can only be 255 characters long.