ibm-information-center/dist/eclipse/plugins/i5OS.ic.cl_5.4.0.1/chgvar.htm

326 lines
18 KiB
HTML

<!doctype html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Change Variable (CHGVAR)</title>
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body bgcolor="white">
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<a name="CHGVAR.Top_Of_Page"></a>
<h2>Change Variable (CHGVAR)</h2>
<table width="100%">
<tr>
<td valign="top" align="left"><b>Where allowed to run: </b>
<ul><li>Batch program (*BPGM)</li>
<li>Interactive program (*IPGM)</li>
</ul><b>Threadsafe: </b>Yes
</td>
<td valign="top" align="right">
<a href="#CHGVAR.PARAMETERS.TABLE">Parameters</a><br>
<a href="#CHGVAR.COMMAND.EXAMPLES">Examples</a><br>
<a href="#CHGVAR.ERROR.MESSAGES">Error messages</a></td>
</tr>
</table>
<div> <a name="CHGVAR"></a>
<p>The Change Variable (CHGVAR) command changes the value of a Control Language (CL) variable or part of a character variable. The value can be changed to the value of a constant, to the value of another variable, or to the value gotten from the evaluation of an expression or a built-in function. Expressions and built-in functions are described in "Expressions in CL Commands" in the CL concepts and reference topic in the iSeries Information Center at http://www.ibm.com/eserver/iseries/infocenter. Also, implicit conversion between decimal and character values is performed by the rules given in the VALUE parameter description.
</p>
<p>The binary built-in function (%BINARY or %BIN) can be used in either the <b>CL variable name (VAR)</b> parameter or the <b>New value (VALUE)</b> parameter as a substitute for a decimal variable. When used with the VAR parameter, the specified portion of the character variable is changed to the signed binary integer equivalent value of the arithmetic expression given in the VALUE parameter. When used within the VALUE parameter, the specified portion of the character variable is treated as a signed binary integer converted to a decimal number when used in evaluating the value of the VALUE parameter. A 2-byte binary integer is converted to a decimal (5 0) number and a 4-byte binary number is converted to decimal (10 0) number. The result of the evaluated expression is then assigned to the specified in the VAR parameter.
</p>
<p>The substring built-in function (%SUBSTRING or %SST) can be used in either the VAR or the VALUE parameter as a substitute for a character variable. When used with the VAR parameter, the specified portion of the character variable is changed to the value of the expression given in the VALUE parameter. When used within the VALUE parameter, the specified portion of the character variable is used in evaluating the value of the VALUE parameter. 2-byte binary integers are converted to decimal (5 0) numbers and 4-byte binary numbers are converted to decimal (10 0) numbers. The result of the evaluated expression is then assigned to the variable specified in the VAR parameter.
</p>
<p>The substring built-in function can be used to retrieve or change all or part of the local data area associated with a job.
</p>
<p>The %SWITCH built-in function can be used in the VALUE parameter as a substitute for a logical variable declared in the program. %SWITCH contains an 8-character mask that indicates which of the eight job switches in a job are tested for 1s and 0s. When %SWITCH is specified for the VALUE parameter, the logical variable specified by the VAR parameter is set to '1' if the logical results of the built-in function are all true. If any of the job switches tested results in a false condition, the variable is set to '0'.
</p>
<p><b>Coding Decimal Values for Decimal Variables</b>
</p>
<p>When a numeric value is specified for a decimal variable:
</p>
<ul>
<li>It can be coded with or without a decimal point (specified as either a period or a comma) and with or without a plus or minus sign.
</li>
<li>If a negative value is specified, a minus sign (-) must precede the value.
</li>
<li>If a decimal point is not specified in the coded value, it is assumed to be on the right of the last digit specified; that is, the coded value is assumed to be an integer (whole number).
</li>
<li>If the number of either integer or fractional digits specified is greater than the defined number of integer or fractional digits, an error message is sent to the user.
</li>
</ul>
<p>For example, if a decimal variable is defined as a five-position decimal value of which two positions are the fraction portion, the following values can be coded:
</p>
<p>
<pre>
Specified Assumed
Value Value
------------ --------
2.7 or 2,7 2.70
27 or 27.00 27.00
-27 -27.00
</pre>
</p>
<p><b>Coding Character Values for Decimal Variables</b>
</p>
<p>When a character value is specified for a decimal variable:
</p>
<ul>
<li>Only the digits 0 through 9, a decimal point (specified as either a period or a comma), and a plus sign (+) or minus sign (-) can be used.
</li>
<li>If a plus sign or minus sign is specified, it must be placed immediately in front of (no blanks between) the first digit in the character value. If no sign character is specified, the value is converted as a positive value.
</li>
<li>The number of decimal positions in the converted result is determined by the decimal point specified in the character value. If no decimal point is specified, it is assumed to be to the right of the last digit in the converted value.
</li>
<li>Decimal alignment occurs in the converted result. The number of decimal positions in the converted result is determined by the number declared for the variable. If the specified character value has more decimal positions than the declared variable, the extra positions on the right are truncated. If the integer portion of the character value has more digits than that declared for the variable, an error message is sent to the user.
</li>
</ul>
<p>The following examples show the results of converting the indicated character values for character variable &amp;A to decimal values for decimal variable &amp;B.
</p>
<p>
<pre>
CHGVAR VAR(&amp;B) VALUE(&amp;A)
</pre>
</p>
<p>
<pre>
| Character Variable &amp;A | Decimal Variable &amp;B |
| | |
|---------+--------------+--------+----------------|
| Length | Specified | Length | Converted |
| | Value | | Result |
|---------+--------------+--------+----------------|
| 10 | '+123.1' | 5, 2 | 123.10 |
| 10 | '+123.00' | 5, 0 | 123 |
| 10 | '-123' | 5, 2 | -123.00 |
|---------+--------------+--------+----------------|
</pre>
</p>
<p>When the binary built-in function is used instead of the decimal variable &amp;B, the decimal value is converted to a signed binary number.
</p>
<p><b> Coding Character Values for Character Variables</b>
</p>
<p>When a character string is specified for a character variable, it must be enclosed in apostrophes if it contains special characters or consists entirely of numeric characters. For example, 'ABC 67', which contains a blank, or '37.92', which contains a decimal point and consists entirely of numeric characters. If 37.92 is not enclosed in apostrophes, it is handled as a decimal value instead of a character value.
</p>
<p>Character variables are padded with blanks (or are truncated) on the right if the character string for the VALUE parameter is shorter (or longer) than the variable specified by the VAR parameter.
</p>
<p>If a character variable is set equal to a portion of another character variable, specify, as parameters on the substring built-in function, the name of the variable containing the substring, the starting character position, and the number of characters being replaced. The starting position and the number of characters can be specified in CL variables.
</p>
<p><b>Coding Decimal Values for Character Variables</b>
</p>
<p>When a decimal value is specified for a character variable:
</p>
<ul>
<li>The same digits, decimal point, and sign character (if the value is negative) are used in the converted result. The value is right-justified in the character variable and padded on the left with zeros, if needed (this is unique to converted CL decimal values).
</li>
<li>The converted result has as many decimal positions as were specified in the decimal value or as defined for the decimal variable being used. If no decimal positions are specified in the decimal value or defined for the decimal variable, no decimal point is placed in the result.
</li>
<li>A minus sign is placed in the leftmost position of the character variable if the specified decimal value is negative. No plus sign is placed in the character variable for positive values.
</li>
</ul>
<p>The following examples show the results of converting the indicated decimal values for decimal variable &amp;B to character values for character variable &amp;A.
</p>
<p>
<pre>
CHGVAR VAR(&amp;A) VALUE(&amp;B)
</pre>
</p>
<p>When the binary built-in function is used instead of the decimal variable &amp;B, the signed binary number is converted to a decimal number.
</p>
<p>
<pre>
| Decimal Variable &amp;B | Character Variable &amp;A |
| | |
|--------+--------------+---------+----------------|
| Length | Specified | Length | Converted |
| | Value | | Result |
|--------+--------------+---------+----------------|
| 5, 2 | 23.00 or +23 | 7 | 0023.00 |
| 5, 2 | -3.9 | 7 | -003.90 |
| 5, 2 | -123.67 | 7 | -123.67 |
|--------+--------------+---------+----------------|
</pre>
</p>
<p>
<b>Note: </b>The character variable must be long enough to accommodate the decimal point and sign character if the value can have a decimal point and a negative value in it. In the last example, although the decimal value is defined as (5, 2), the character variable must be at least 7 characters long for the value shown. In the next-to-last example, the character variable could only be 5 characters long and the converted result -3.90 would be valid.
</p>
<p>The substring built-in function can be used to change a substring of a character variable specified in the VAR parameter to a decimal value in the VALUE parameter.
</p>
<p><b>Coding Logical or Character Values for Logical Variables</b>
</p>
<p>The value for a logical variable must be a logical value of either '1' or '0'. It must be enclosed in apostrophes. However, the %SWITCH built-in function can be used in place of a logical variable in the VALUE parameter. Refer to for a description of the %SWITCH built-in function.
</p>
<p>
<b>Note: </b>Values for decimal and character variable types can be specified in hexadecimal form (X'580F' for decimal 58.0). However, if character values are specified in hexadecimal form, care should be used because no validity checking is performed on the hexadecimal string.
</p>
<p><b>Restrictions:</b>
</p>
<ul>
<li>The CHGVAR command is valid only in CL procedures.
</li>
</ul>
</div>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div>
<h3><a name="CHGVAR.PARAMETERS.TABLE">Parameters</a></h3>
<table border="1" cellpadding="4" cellspacing="0">
<!-- col1="10" col2="15" col3="30" col4="10" -->
<tr>
<th bgcolor="aqua" valign="bottom" align="left">Keyword</th>
<th bgcolor="aqua" valign="bottom" align="left">Description</th>
<th bgcolor="aqua" valign="bottom" align="left">Choices</th>
<th bgcolor="aqua" valign="bottom" align="left">Notes</th>
</tr>
<tr>
<td valign="top"><a href="#CHGVAR.VAR"><b>VAR</b></a></td>
<td valign="top">CL variable name</td>
<td valign="top"><i>CL variable name</i></td>
<td valign="top">Required, Positional 1</td>
</tr>
<tr>
<td valign="top"><a href="#CHGVAR.VALUE"><b>VALUE</b></a></td>
<td valign="top">New value</td>
<td valign="top"><i>Character value</i></td>
<td valign="top">Required, Positional 2</td>
</tr>
</table>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
</div>
<div> <a name="CHGVAR.VAR"></a>
<h3>CL variable name (VAR)</h3>
<p>Specifies the CL variable whose value is to be changed. The type of variable does not have to be the same as the type of constant or variable specified in the VALUE parameter, unless an expression is being evaluated or the VAR parameter specifies a logical variable.
</p>
<p>If the substring built-in function or the binary built-in function is used to change a portion of a character variable (that is, a substring of the character string in the variable) specified in VAR to a value specified in the VALUE parameter, specify the name of the character variable, followed by the starting position and the number of characters being changed within the character string specified by the variable name.
</p>
<p>This is a required parameter.
</p>
</div>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
<div> <a name="CHGVAR.VALUE"></a>
<h3>New value (VALUE)</h3>
<p>Specifies the expression that is used to change the value of the variable. Variables, constants, or a built-in function can be used within the expression. For a description of expressions, see "Expressions in CL Commands" in the CL concepts and reference topic in the iSeries Information Center at http://www.ibm.com/eserver/iseries/infocenter.
</p>
<p>If a constant is used as a simple expression, its value must be specified by the following rules, depending on the type of constant being specified and whether the variable was declared as a decimal, character, or logical variable.
</p>
<p>This is a required parameter.
</p>
</div>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div><h3><a name="CHGVAR.COMMAND.EXAMPLES">Examples</a> </h3>
<p><b>Example 1: Changing Decimal Variables</b>
</p>
<p>
<pre>
CHGVAR &amp;A &amp;B
</pre>
</p>
<p>The value of variable &amp;A is set to the value of the variable &amp;B. If &amp;B has a value of 37.2, then the value of &amp;A becomes 37.2 also.
</p>
<p>
<pre>
CHGVAR &amp;Y (&amp;Y + 1)
</pre>
</p>
<p>The value of variable &amp;Y is increased by 1. If &amp;Y has a value of 216, its value is changed to 217.
</p>
<p><b>Example 2: Changing Logical Variables</b>
</p>
<p>
<pre>
CHGVAR &amp;X (&amp;Y *OR &amp;Z)
</pre>
</p>
<p>The value of the logical variable &amp;X is set to the value of the result of the OR operation of the logical variable &amp;Y with the logical variable &amp;Z. Both variables <u> must</u> be logical variables when *OR is used. If &amp;Y equals '0' and &amp;Z equals '1', then &amp;X is set to '1'.
</p>
<p>
<pre>
CHGVAR &amp;A %SWITCH(10XXXX10)
</pre>
</p>
<p>The value of the logical variable &amp;A is determined by the logical results of the built-in function, %SWITCH. Positions 1, 2, 7, and 8 of the 8-character mask indicate that the corresponding job switches for the job are to be tested for the values indicated in the mask. Job switches 1 and 7 are tested for 1s, and switches 2 and 8 are tested for 0s. (Switches 3 through 6 are not tested.) If all four switches contain the values specified in the %SWITCH mask, the logical result of the built-in function is true, and the variable &amp;A is set to a '1'. If any of the four switches contain a value not indicated in the mask, the result is false and &amp;A is set to '0'.
</p>
<p><b>Example 3: Changing Character Variables</b>
</p>
<p>
<pre>
CHGVAR VAR(&amp;A) VALUE(AB *CAT CD)
CHGVAR &amp;A ('AB' *CAT 'CD')
</pre>
</p>
<p>These two commands set the value of the variable &amp;A equal to the character string ABCD, which is the result of the concatenation of the two character strings AB and CD. The first command is coded in keyword form with unquoted strings; the second command is coded in positional form with the VALUE parameter specifying two quoted character strings.
</p>
<p>
<pre>
CHGVAR &amp;VAR1 &amp;VAR2
</pre>
</p>
<p>This example shows a 6-character variable whose value is changed by a shorter character string. If &amp;VAR1 = ABCDEF and &amp;VAR2 = XYZ before the command is processed, the result in &amp;VAR1 = XYZ padded on the right with three blanks.
</p>
<p>
<pre>
CHGVAR &amp;VAR1 '12'
</pre>
</p>
<p>Assuming &amp;VAR1 is a character variable that is 6 characters long, the result in &amp;VAR1 = 12 padded on the right with four blanks. The apostrophes are required in this example.
</p>
<p>
<pre>
CHGVAR VAR(%SUBSTRING(&amp;A 4 3)) VALUE(REP)
or
CHGVAR VAR(%SST(&amp;A 4 3)) VALUE(REP)
</pre>
</p>
<p>The substring built-in function is used to change 3 characters of the character constant in the variable named &amp;A. If &amp;A has a value of ABCDEFGH, the fourth, fifth, and sixth characters in &amp;A are set to REP, and the result is ABCREPGH.
</p>
<p>
<pre>
CHGVAR VAR(%SST(*LDA 1 512)) VALUE(' ')
</pre>
</p>
<p>The substring built-in function is used to change all of the local data area to blanks.
</p>
<p>
<pre>
CHGVAR VAR(%BINARY(&amp;A 1 2)) VALUE(20)
</pre>
</p>
<p>or
</p>
<p>
<pre>
CHGVAR VAR(%BIN(&amp;A 1 2)) VALUE(20)
</pre>
</p>
<p>The binary built-in function is used to change the first 2 characters of the character variable named &amp;A to the signed binary value of the number 20, or hexadecimal number X'0014'. If the character variable named &amp;A has a length of 10, characters 3 through 10 of variable &amp;A are not changed.
</p>
</div>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
<hr size="2" width="100%">
<div><h3><a name="CHGVAR.ERROR.MESSAGES">Error messages</a> </h3>
<p><b><u>*ESCAPE Messages</u></b>
</p>
<dl>
<dt><b>CPF0816</b></dt>
<dd>%SWITCH mask &amp;1 not valid.
</dd>
</dl>
</div>
<table width="100%">
<tr><td align="right"><a href="#CHGVAR.Top_Of_Page">Top</a></td></tr>
</table>
</body>
</html>