Assignment statements
Assignment statements move data, format data, and change the content of specified variables. This topic discusses the following ways in which data can be moved:
- Moving alpha data to an alpha destination
- Moving alpha data to a numeric destination
- Moving numeric data to an alpha destination
- Moving decimal/packed data to a decimal/packed destination
- Moving decimal/packed data to an implied-decimal/implied-packed destination
- Moving decimal/packed data to an integer destination
- Moving implied-decimal/implied-packed data to an implied-decimal/implied-packed destination
- Moving implied-decimal/implied-packed data to a decimal/packed destination
- Moving implied-decimal/implied-packed data to an integer destination
- Moving integer data to a decimal/packed destination
- Moving integer data to an implied-decimal/implied-packed destination
- Moving integer data to an integer destination
- Assigning an object handle to another object handle
Several forms of assignment statements exist. Generally, an assignment statement has the following format:
destination = source
Either a simple, subscripted, or ranged variable whose content is to be modified.
An expression whose result is assigned to destination.
Because Synergy DBL evaluates any expressions contained in source before it alters the value of destination, the same variable can be present as both destination and part of source. For example, the following assignment statement multiplies the current value of decimal_var by 3 and then assigns the result as the new value for decimal_var:
decimal_var = decimal_var * 3
By default, Synergy DBL rounds results when storing from an implied-decimal source expression. If you want Synergy DBL to truncate results instead of rounding them in these situations, you can use the %TRUNCATE function or (in traditional Synergy only) specify the TRUNCATE option on MAIN, FUNCTION, and SUBROUTINE statements or set system option #11. See %TRUNCATE, MAIN-ENDMAIN, FUNCTION-ENDFUNCTION, SUBROUTINE-ENDSUBROUTINE, and system option #11.
Moving alpha data to an alpha destination
When the destination and source are both alpha, the result of the source is copied to the destination. If the source is shorter than the destination, its result is copied left-justified with trailing blanks into the destination. If the source is longer than the destination, only the leftmost characters are copied to the destination, and no warning or error occurs.
If you include a justification format in an alpha-to-alpha assignment, the source is treated as decimal. See Controlling justification for information about justification controls.
Below are examples of alpha-to-alpha assignments and their corresponding results:
record xyz result ,a4 afld1 ,a6, "abcdef" afld2 ,a2, "xy" proc ;The results will be: result = afld2 ;"xy " result = afld1 ;"abcd" (because result is truncated) result = afld1(4:5) ;"defx" (because ranging spans afld1 ; [starting with 4th character] and ; afld2 and result is truncated) result = "1234" ;"1234" stop end
Moving alpha data to a numeric destination
When the destination is numeric and the source is alpha, the source is evaluated to produce a resulting numeric value. The expression is then moved according to the rules for moving a source of the resulting data type to the data type of the destination.
If a character that is not a blank, a decimal point, a sign (+ or –), or a numeric digit is encountered during the conversion process, a “Bad digit encountered” error ($ERR_DIGIT) occurs. (See system option #37 for more information.) Sign characters can occur at any position within the alpha source and are applied in the order in which they occur. Blanks and plus signs (+) are ignored, and a single decimal point is allowed.
If a decimal point is present, the evaluated result is implied-decimal. The implied-decimal expression then moves according to the rules for moving an implied-decimal source to an implied-decimal destination. If no decimal point is present, the evaluated result is decimal. The decimal expression then moves according to the rules for moving an alpha source to a decimal destination.
Below are examples of alpha-to-decimal assignments and their corresponding results:
main a_to_n record results decvar ,d6 impvar ,d5.3 int4var ,i4 int2var ,i2 int1var ,i1 proc decvar = "-1-2-3" ;-123 decvar = "123456789" ;456789 (leftmost digits lost) decvar = " 3 5 8 " ;358 decvar = "9.78" ;10 if compiled with DBLOPT #11 not set ;9 if compiled with DBLOPT #11 set decvar = "abcde" ;"Bad digit encountered" error impvar = " 6448.3" ;48.300 (leftmost digits lost) impvar = "54.32" ;54.320 impvar = "19.3927" ;19.393 if compiled with DBLOPT #11 not set ;19.392 if compiled with DBLOPT #11 set int1var = "456" ;-56 (high-order byte of integer lost) int2var = "-231.796" ;-232 if compiled with DBLOPT #11 not set ;-231 if compiled with DBLOPT #11 set int4var = "123456789" ;123456789 endmain
Moving numeric data to an alpha destination
When the destination is alpha and the source is numeric, the assignment represents a formatting operation. The value of the decimal expression is loaded into the alpha variable’s data area according to whether the format is implicitly or explicitly specified as part of the statement.
If the source is integer, packed, or implied-packed, it is first converted to decimal. |
Implicit formatting rules
If the source is a numeric expression, the value is transferred to the destination according to the following rules:
- The significant digits of the value are loaded right-justified over leading blanks.
- If the value is negative, a minus sign is loaded immediately in front of the leftmost digit.
- If the source is implied-decimal, the decimal point character is inserted between the whole number part and the fractional precision.
- If the number of digits and any decimal point in the source value is larger than the size of the destination, only the rightmost part is transferred. If the value is negative, the minus sign is not loaded, and no warning or error occurs.
- If the number of digits and any decimal point in the value is the same as the size of the destination, all digits of the value are transferred. If the value is negative, the minus sign is not loaded, and no warning or error occurs.
Below are examples of the implicit formatting of decimal-to-alpha assignments and their corresponding results:
main n_to_a record results alpha6 ,a6 alpha9 ,a9 record source decfld1 ,d3, -23 decfld2 ,d6, -123456 impfld1 ,d4.2, 68.54 impfld2 ,d12.4, 12345678.9876 intfld1 ,i1, 99 intfld2 ,i2, 1003 intfld4 ,i4, 82355623 proc open(1, o, "tt:") alpha6 = decfld1 ;" -23" alpha6 = decfld2 ;"123456" (minus is lost) alpha6 = impfld1 ;" 68.54" alpha6 = impfld2 ;"8.9876" (leftmost digits lost) alpha9 = decfld2 ;" -123456" alpha9 = impfld2 ;"5678.8976" alpha6 = intfld1 ;" 99" alpha6 = intfld2 ;" 1003" alpha6 = intfld4 ;"355623" (leftmost digits lost) alpha9 = intfld4 ;" 82355623" endmain
If the source is an arithmetic expression followed by a comma and an alpha expression, the assignment contains an explicit format specification. The loading of the destination is determined by a format specification in the form
destination = source, format
If the formatted result is shorter than the destination, it is loaded right-justified over blanks. If the formatted result is longer than the destination, only the rightmost portion is loaded, and no warning or error occurs.
The format specification is an alpha field or literal. It is a sequence of characters that depicts how the destination will look. The following characters are case sensitive and have special meanings. Any other character that appears in a format string is loaded directly into the destination.
Character |
Meaning |
---|---|
X |
Represents a single digit. It causes a digit from the source data to be loaded into the specified position in the destination. Digits are extracted from the source beginning at the right and continuing to the left, with leading zeros ignored. Thus, the rightmost X in the format receives the rightmost digit in the source. If more X characters are specified than there are digits in the source, the leftmost X positions are loaded with zeros. The X character must be uppercase. Any Z or asterisk (*) formatting character that appears to the right of an X is treated as an X character. |
Z |
Represents a digit position and is functionally similar to X, except when more Z characters are specified than there are significant digits in the source data, they are loaded with blanks. If a period appears to the left but not to the right, or if an X appears to the left of the Z, the Z position is treated as an X. The Z character must be uppercase. |
* (asterisk) |
Represents a digit position. It is similar to an X character, except that when there are no more significant digits to be transferred, the position is loaded with an asterisk rather than a zero. If an asterisk is to the left of a comma when there are no more digits to be transferred, the comma is replaced with an asterisk in the destination. |
money sign |
Represents a digit position. This format character is similar to a Z character in that it causes a digit from the source to be loaded into the specified position of the destination, and when there are no more significant digits to be transferred, the position is loaded with a money sign. By default, the money sign is “$”, but the MONEY or LOCALIZE subroutine can change it to any character you want. To prevent possible confusion in formatting, do not designate any of the standard formatting characters in this section to be money signs. |
– (minus sign) |
If used as the first or last character in a format, it causes a minus sign to be loaded at the corresponding position in the destination when the source is negative. If the source is positive, a blank is loaded. If the minus sign is used within the format, a hyphen (–) is loaded at the corresponding position in the destination. |
. (period) |
Causes a decimal point to be loaded at the corresponding position in the destination. Any Z character that appears to the right of the decimal point is treated as an X. |
, (comma) |
Causes a comma to be loaded at the corresponding destination position under the following conditions:
|
If the Z and X formatting characters are not uppercase, they’ll be loaded into the destination. |
Format characters should be considered “reserved” characters in format strings that contain both text and format characters: Do not use a format character as normal text. For example, the second period in the following format string causes unexpected results:
"XX.XX lbs."
In some parts of the world, the use of commas and periods is reversed. Use the LOCALIZE subroutine to substitute appropriate formatting characters. |
To avoid the overhead of explicit formatting controls, use casting to place the decimal point in the destination string. This strategy honors the decimal point values, whether or not they are set in the FLAGS or LOCALIZE subroutines, although it does not insert “thousands” separators. record number ,d6 result ,a10 proc number = 12345 result = ^d(number, 2) ;Result is " 123.45" stop end |
Below are examples of ways to use the format characters in decimal-to-alpha assignments:
record alpha ,a10 proc ;Results will be: alpha = 987, "XXXXXX-" ;" 000987 " alpha = -987, "XXXXXX-" ;" 000987-" alpha = 987, "-XXX" ;" 987" alpha = 987, "XXXXXX" ;" 000987" alpha = 987, "ZZZZZZ" ;" 987" alpha = -987, "-ZZZZZZ" ;" - 987" alpha = 987, "******" ;" ***987" alpha = 98765, "Z,ZZZ,ZZZ" ;" 98,765" alpha = 98765, "*,***,***" ;" ***98,765" alpha = 9, "***.**" ;" ***.09" alpha = 9876, "$$$,$$$.$$" ;" $98.76" alpha = 9876, "$$*,***.XX" ;" $***98.76" alpha = 9876, "Val: Z.ZZ" ;"Val: 8.76" alpha = 95, "This puts a X in" ;"This puts a 5 in" stop end
By default, Synergy DBL loads the formatted information in numeric-to-alpha assignments right-justified over blanks. You can change this default and load formatted information left-justified, however, by including a justification control as the last item in the assignment statement as follows:
statement[ [justification[:variable]]]
statement
A numeric-to-alpha assignment statement.
justification
(optional) Either LEFT or RIGHT (default).
variable
(optional) Updated with the number of characters of the formatted information that are loaded into the destination, excluding any leading blanks. (n)
Below are examples of controlling the justification of implicitly and explicitly formatted numeric-to-alpha assignments. Each example uses the following data division:
record alpha ,a10 len ,i2
In the following example, the first two statements don’t affect the value of len. The third statement, however, assigns len the value of 5.
proc ;The results will be: alpha = 12345 ;" 12345" alpha = 12345 [LEFT] ;"12345 " alpha = 12345 [RIGHT:len] ;" 12345" stop end
In the following example, the third statement assigns len the value of 7. Note that the trailing blank is included.
proc ;The results will be: alpha = 12345, "ZZ,ZZZ.ZZ-" ;" 123.45 " alpha = 12345, "ZZ,ZZZ.ZZ-" [LEFT] ;"123.45 " alpha = 12345, "ZZ,ZZZ.ZZ-" [RIGHT:len] ;" 123.45 " stop end
In the following example, the second statement assigns len a value of 5.
proc ;The results will be: alpha = 12345 [LEFT] ;"12345 " alpha = 12345 [LEFT:len] ;"12345 " stop end
In the following example, the third statement assigns len a value of 6.
proc ;The results will be: alpha = 12345, "ZZ,ZZZ.ZZ-" ;" 123.45 " alpha = 12345, "ZZ,ZZZ.ZZ-" [LEFT] ;"123.45 " alpha = 12345, "ZZ,ZZZ.ZZ-" [LEFT:len] ;"123.45 " stop end
Moving decimal/packed data to a decimal/packed destination
When the destination is decimal or packed and the source is decimal or packed, the result of the source is copied to the destination. If the size of the source is less than the size of the destination, its result is loaded right-justified over leading zeros. If the size of the source is greater than the size of the destination, the leftmost digits are truncated, and no warning or error occurs.
Below are examples of decimal-to-decimal assignments and their corresponding results:
record result ,d6 proc ;The results will be: result = 123 ;000123 result = 1234567 ;234567 (leftmost digit lost) stop end
Moving decimal/packed data to an implied-decimal/implied-packed destination
When the destination is implied-decimal or implied-packed and the source is decimal or packed, the result of the source is copied to the whole number part of the destination. If the value of the expression is less than the size of the destination, the value is loaded right-justified over leading zeros. If it is greater than the size of the destination, the leftmost digits are lost and no warning or error occurs. In both cases, zeros are loaded to the right of the decimal point. The sign is maintained.
Below are examples of decimal-to-implied-decimal assignments and their corresponding results:
record result ,d6.2 dfld1 ,d4, 1234 dfld2 ,d6, -567890 proc ;The results will be: result = 2*dfld1 ;2468.00 result = dfld2 ;-7890.00 (leftmost digits lost) result = dfld1/3 ;0411.00 (source was decimal, so value ; was truncated before assignment ; took place) result = ^d(dfld1,0)/3 ;411.33 source (source was casted as ; implied-decimal, so decimal places ; were preserved) stop end
Moving decimal/packed data to an integer destination
When the destination is integer and the source is decimal or packed, the binary representation of the actual value of source is copied to the destination.
If the number of bytes to be moved from the source is greater than the total number of bytes in the destination, the high-order bits from the source that cause the overflow are ignored and the sign may change. If the number of bits to be moved from the source is less than the total number of bits in the destination, sign extension occurs.
Below are examples of decimal-to-integer assignments and their corresponding results:
record result1 ,i1 result2 ,i4 dfld1 ,d3, 999 ;Hex 3E7 dfld2 ,d5, 28777 ;Hex 7069 dfld3 ,d10, 4502228111 ;Hex 10C5A8C8F proc ;The results will be: result1 = dfld1 ;-25, Hex E7 result1 = dfld2 ;105, Hex 69 result1 = dfld3 ;-113, Hex 8F result2 = dfld1 ;999, Hex 000003E7 result2 = dfld2 ;28777, Hex 00007069 result2 = dfld3 ;207260815, Hex 0C5A8C8F stop end
Moving implied-decimal/implied-packed data to an implied-decimal/implied-packed destination
When the destination and source are both implied-decimal or implied-packed (or one of each), the result of the source is copied to the destination.
The decimal point of the source is aligned to that of the destination. The whole number part of the source is copied to the left of the destination’s decimal point, right-justified over leading zeros. If there isn’t enough room to load all of the whole number digits, they are truncated at the left, and no warning or error occurs.
The fractional part of the source is copied to the right of the destination’s decimal point, left-justified over trailing zeros. If there isn’t enough room to load all of the decimal place digits, the source fractional part is rounded, and no warning or error occurs. (See MAIN-ENDMAIN and system option #11 for more information on changing the default from rounding to truncating.) The sign is maintained.
Below are examples of implied-decimal-to-implied-decimal assignments and their corresponding results (with rounding in effect):
record result ,d6.2 idfld1 ,d6.1, 12345.6 idfld2 ,d4.3, 9.876 proc ;The results will be: result = idfld1 ;2345.60 (leftmost digits lost) result = idfld2 ;0009.88 (rightmost digits lost) result = idfld2 - dfld1 ;-2335.72 (left and rightmost stop ; digits lost) end
Moving implied-decimal/implied-packed data to a decimal/packed destination
When the destination is decimal or packed and the source is implied-decimal or implied-packed, the whole number part of the source is copied to the destination. By default, the value of the implied expression is rounded to a whole number. (See MAIN-ENDMAIN and system option #11 for more information on changing the default from rounding to truncating.) If the value of the expression is less than the size of the destination, the value is loaded right-justified over leading zeros. If the expression is greater than the size of the destination, the leftmost digits are lost, and no warning or error occurs. The sign is maintained.
Below are examples of implied-decimal-to-decimal assignments and their corresponding results (with rounding in effect):
record result ,d4 idfld1 ,d4.2, 98.12 idfld2 ,d7.2, 12345.67 proc ;The results will be: result = idfld1 ;0098 result = idfld2 ;2346 (leftmost digits lost) stop end
Moving implied-decimal/implied-packed data to an integer destination
When the source is implied-decimal or implied-packed and the destination is integer, the binary representation of the whole number part of the source is copied to the destination. By default, the value of the implied expression is rounded to a whole number. (See MAIN-ENDMAIN and system option #11 for more information on changing the default from rounding to truncating.)
If the number of bits to be moved from the source is greater than the total number of bits in the destination, the high-order bits from source that cause the overflow are ignored and the sign may change. If the number of bits to be moved from the source is less than the total number of bits in the destination, sign extension occurs.
Below are examples of implied-decimal-to-integer assignments and their corresponding results:
record result1 ,i1 result2 ,i4 idfld1 ,d4.2, 8.72 idfld2 ,d5.2, 535.99 idfld3 ,d10.4, 852436.0292 proc ;The results will be: result1 = idfld1 ;9, Hex 9 result1 = idfld2 ;24, Hex 18 result1 = idfld3 ;-44, Hex D4 result2 = idfld1 ;9, Hex 00000009 result2 = idfld2 ;536, Hex 00000218 result2 = idfld3 ;852436, Hex 000D01D4 stop end
Moving integer data to a decimal/packed destination
When the destination is decimal and the source is integer, the ASCII representation of the value of the source is copied to the destination. When the destination is packed and the source is integer, the compressed BCD representation of the value of the source is copied to the destination. The destination is loaded right-justified over leading zeros. If the destination is too small to load the source’s digits, the leftmost digits are lost, and no warning or error occurs. The sign is maintained.
Below are examples of integer-to-decimal assignments and their corresponding results:
record result ,d6 ifld1 ,i1, 46 ifld2 ,i2, 8220 ifld4 ,i4, 1230984 proc ;The results will be: result = ifld1 ;000046 result = ifld2 ;008220 result = ifld4 ;230984 (leftmost digit lost) stop end
Moving integer data to an implied-decimal/implied-packed destination
When the destination is implied-decimal and the source is integer, the ASCII representation of the value of the source is copied to the destination. When the destination is implied-packed and the source is integer, the compressed BCD representation of the value of the source is copied to the destination.
If the value of the source is less than the size of the destination, the value is loaded right-justified over leading zeros. If it is greater than the size of the destination, the leftmost digits are lost, and no warning or error occurs. In both cases, zeros are loaded to the right of the decimal point. The sign is maintained.
Below are examples of integer-to-implied-decimal assignments and their corresponding results:
record result ,d6.2 ifld1 ,i1, 65 ifld2 ,i2, 5000 ifld4 ,i4, 9232417 proc ;The results will be: result = ifld1 ;0065.00 result = ifld2 ;5000.00 result = ifld4 ;2417.00 (leftmost digits lost) stop end
Moving integer data to an integer destination
When the destination and source are both integer, the significant bits of the source are copied to the low-order bits of the destination.
If the number of bits to be moved from the source is greater than the total number of bits in the destination, the high-order bits from the source that cause the overflow are ignored and the sign may change. If the number of bits to be moved from the source is less than the total number of bits in the destination, sign extension occurs.
Below are examples of integer-to-integer assignments and their corresponding results:
record result1 ,i1 result2 ,i2 result4 ,i4 ifld1 ,i1, -99 ;Hex 9d ifld2 ,i2, 1003 ;Hex 03EB ifld4 ,i4, 8235623 ;Hex 007DAA67 proc ;The results will be: result1 = ifld1 ;-99, Hex 9D result1 = ifld2 ;-21, Hex EB result1 = ifld4 ;103, Hex 67 result2 = ifld1 ;-99, Hex FF9D result2 = ifld2 ;1003, Hex 03EB result2 = ifld4 ;-21913, Hex AA67 result4 = ifld1 ;-99, Hex FFFFFF9D result4 = ifld2 ;1003, Hex 000003EB result4 = ifld4 ;8235623, Hex 007DAA67 stop end
Assigning an object handle to another object handle
You can assign one object handle to another object handle if the destination object type matches or is a parent of the object type of the source object instance. For the following example, assume class4 inherits from class3:
c3 ,@class3 c4 ,@class4 c3 = new class3() ;Valid c3 = new class4() ;Valid c4 = new class4() ;Valid c4 = new class3() ;Invalid
If you assign one non-null object handle (for example, var1) to another object handle (var2), the reference to var1’s object is automatically cleared, and var1 now references var2’s object.
In the assignment below, a new string object is created that contains “abc”.
strfld = "abc"
When assigning one handle to another, as in the example below, the compiler ensures that the right side can be assigned to the left side.
hand1 = hand2
If the right side can be assigned to the left side, the following occurs at runtime:
- The reference to hand1’s original object, if any, is released, which may in turn cause the destruction of the object pointed to by hand1 (if it was the last reference).
- The reference to hand2 from hand1 is added.
The following example makes an equality test that will use the op_Equality method:
if (obj == 10)
In an IF statement like the latter example, if an object is on either side of the equality operator (or any Boolean-style operator), the compiler searches its class type for an op_Equality (or the corresponding op_ operator). The compiler looks for a public static method with the specified name that has two parameters that match the types on both sides of the operator. If the method is found, it is changed into a method call of that operator. If the right side is a different type, that type is also searched to find the operator.
The following assignment isn’t possible, because you cannot assign an int to an object. A compiler error would be generated for this statement:
hand1 = int