8.8 Dynamic Mapping
Dynamic mapping lets you redefine the position of vari-
ables in a static storage area. This storage area can be either
a map name or a previously declared static string variable.
Dynamic mapping requires the following BASIC statements:
.
A declarative statement, such as a MAP statement,
allocating a fixed-length storage area
.
A MAP DYNAMIC statement, naming the variables
whose positions can change at run time
.
A REMAP statement, specifying the new positions of the
variables named in the MAP DYNAMIC statement
The MAP DYNAMIC statement does not affect the amount
of storage allocated. The MAP DYNAMIC statement causes
BASIC to create internal pointers to the variables and ar-
ray elements. Until your program executes the REMAP
statement, the storage for each variable and each array ele-
ment named in the MAP DYNAMIC statement starts at the
beginning of the map storage area.
The MAP DYNAMIC statement is nonexecutable. With this
statement, you cannot specify a string length. All string items
have a length of zero until the program executes a REMAP
statement.
The REMAP statement specifies the new positions of vari-
ables named in the MAP DYNAMIC statement. That is, it
causes BASIC to change the internal pointers to the data.
Because the REMAP statement is executable, it can rede-
fine the pointer for a variable or array element each time the
REMAP statement is executed.
With the MAP DYNAMIC statement, you can specify either
a map name or a previously declared static string variable.
When you specify a map name, a MAP statement with the
same map name must lexically precede the MAP DYNAMIC
statement.
In the following example, the MAP statement creates a
storage area named emp_buffer . The MAP DYNAMIC
statement specifies that the positions of variables emp_name
and emp_address within the map area can be dynamically
defined with the REMAP statement.
EXAMPLE: Click to display example.
At the start of program execution, the storage for badge is the
first 4 bytes of emp_buffer , the storage for social_sec_num
is equal to 9 bytes and together name_length and address_
length are equal to 2 bytes. The FILL keyword reserves 60
additional bytes of storage. The MAP DYNAMIC statement
defines the variables emp_name and emp_address whose po-
sitions and lengths will change at run time. When executed,
the REMAP statement defines the FILL area to be equal to
emp_fixed_info and defines the positions and lengths of emp_
name and emp_address .
When you specify a static string variable, it must be either
a variable declared in a MAP or COMMON statement or a
parameter declared in a SUB, FUNCTION, PICTURE, or
DEF. The actual parameter passed to the procedure must
be a static string variable defined in a COMMON, MAP, or
RECORD statement.
The following example shows the use of a static string vari-
able as a parameter declared in a SUB. The MAP DYNAMIC
statement specifies the input parameter, input_rec , as the
string to be dynamically defined with the REMAP statement.
In addition, the MAP DYNAMIC statement specifies a string
array A whose elements will point to positions in input_
rec after the REMAP statement is executed. The REMAP
statement defines the length and position of each element
contained in array A . The FOR...NEXT loop then assigns
each element contained in array A into array item , the target
array.
SUB deblock (STRING input_rec, STRING item())
MAP DYNAMIC (input_rec) STRING A(1 TO 3)
REMAP (input_rec) &
A(1) = 5, &
A(2) = 3, &
A(3) = 4
FOR I = LBOUND(A) TO UBOUND(A)
item(I) = A(I)
NEXT I
END SUB
Note that dynamic map variables are local to the program
module in which they reside. Therefore, REMAP only affects
how that module views the buffer.
For more information about using the MAP DYNAMIC and
REMAP statements, see the DEC BASIC and VAX BASIC
for OpenVMS Systems Reference Manual .