Assignment

Purpose

Assigns a value to one or more specified variables.

Syntax

target[,targetn]… =expression[,BY NAME];

Parameters:

target(s)
Either variable references or pseudovariables.
expression
Any valid expression.

Description

The assignment statement evaluates expression and assigns its value to the specified variable or variables.

The assignment statement evaluates the target references and the expression in an unspecified order, converts the expression's value to the data type of each target, and then assigns the converted value to each of the targets in turn (in left-to-right order). For the rules for converting data types, see the chapter Data Type Conversions.

Assignments to a bit-string or character-string target that is shorter than the converted source value cause truncation of the rightmost bits or characters of the source value.

Assignments to nonvarying character-string targets result in padding of the converted source value on the right with sufficient blanks to make it the length of the target.

Assignments to a varying character-string target cause the target to obtain the current length of the newly assigned value.

Assignments to bit-string targets result in padding of the source value on the right with sufficient zero bits to make it the length of the target.

The value of expression can be assigned to more than one target. If the assignment is a structure assignment using the BY NAME option, such a multiple assignment affects only those elementary items that have names in common in all of the structures on both sides of the statement.

The BY NAME option is used in structure assignments to assign to the target structure only the values of those items in the structures specified in expression that have names in common with items contained in the target. The BY NAME option can be used only when the right-hand side contains at least one structure. In such a case, the BY NAME structure assignment is equivalent to a series of scalar or array assignments. There is one such assignment for each scalar or array member of the target structure for which the following condition is true: every structure referenced in the right-hand side has a corresponding scalar or array member whose full structure name (except for the top-level structure name) is identical to the target member's full structure name (except for its top-level structure name). These separate assignments are generated in the order in which the scalar and array members occur in the target structure. For example:

DECLARE 1 STRUC1, 
   2 A … ,
   2 B,
      3 M … ,
      3 N … ,
      3 O … ,
   2 C … ,
   2 D … ;

DECLARE 1 STRUC2,
   5 C … ,
   5 D … ,
   5 B,
      10 L … ,
      10 M … ,
      10 O … ;

DECLARE 1 STRUC3, 
   2 A … ,
   2 D … ,
   2 C … ,
   2 E,
      3 M … ,
      3 N … ,
      3 O … ,
   2B,
      3 M … ,
      3 N … ;

STRUC1 = STRUC2 + STRUC3, BY NAME;

This assignment is equivalent to the series of assignments:

STRUC1.B.M = STRUC2.B.M + STRUC3.B.M; 
STRUC1.C = STRUC2.C + STRUC3.C; 
STRUC1.D = STRUC2.D + STRUC3.D;
Note: BYNAME is accepted as a synonym for BY NAME.

Examples

A = B+C;
A,B = B*C;   /*A and B will each have the value of B*C */ 
X(K) = 5;
P->NODE.VALUE = SQRT(X(J));
SUBSTR(S,I,J) = 'ABC';

Restriction

In case of array assignment, each target variable must be an array. The right-hand side can be an array or element expression. If the right-hand side contains an array, its shape must be identical to the shape of the target array.

In case of structure assignment, each target variable must be a structure. The right-hand side can be a structure or element expression. If the right-hand side contains a structure, its shape must be identical to the shape of the target structure.

Because the target and the expression are evaluated in an unspecified order, functions or ON-units called during these evaluations may be affected by possible side effects, such as assignments to subscripts or pointers used in the target reference or freeing the target's storage. Likewise, the storage of the target must not be freed by the execution of a function or ON-unit called during evaluation of the expression.

Programs that depend on the order of evaluation may fail when moved to other implementations of PL/I.