ibm-information-center/dist/eclipse/plugins/i5OS.ic.rbam6_5.4.0.1/subst.htm

178 lines
9.8 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="%SUBSTRING built-in function" />
<meta name="abstract" content="The substring built-in function (%SUBSTRING or%SST) produces a character string that is a subset of an existing character string and can only be used within a CL procedure." />
<meta name="description" content="The substring built-in function (%SUBSTRING or%SST) produces a character string that is a subset of an existing character string and can only be used within a CL procedure." />
<meta name="DC.subject" content="%SUBSTRING (substring) built-in function, description, %SST (substring) function, substring function, example, SST function, substring function" />
<meta name="keywords" content="%SUBSTRING (substring) built-in function, description, %SST (substring) function, substring function, example, SST function, substring function" />
<meta name="DC.Relation" scheme="URI" content="contp.htm" />
<meta name="DC.Relation" scheme="URI" content="usvar.htm" />
<meta name="DC.Relation" scheme="URI" content="valuv.htm" />
<meta name="DC.Relation" scheme="URI" content="../clfinder/finder.htm" />
<meta name="DC.Relation" scheme="URI" content="../cl/if.htm" />
<meta name="DC.Relation" scheme="URI" content="../cl/chgvar.htm" />
<meta name="DC.Relation" scheme="URI" content="rbam6builtinfunc.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="subst" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>%SUBSTRING built-in function</title>
</head>
<body id="subst"><a name="subst"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">%SUBSTRING built-in function</h1>
<div><p>The substring built-in function (%SUBSTRING or%SST) produces a
character string that is a subset of an existing character string and can
only be used within a CL procedure. </p>
<div class="section"><p>In a <span class="cmdname">Change Variable (CHGVAR)</span> command, the
%SST function can be specified in place of the variable (VAR parameter) to
be changed or the value (VALUE parameter) to which the variable is to be changed.
In an <span class="cmdname">If (IF)</span> command, the %SST function can be specified
in the expression.</p>
<div class="p">The format of the substring built-in
function is shown in this example: <pre>%SUBSTRING(character-variable-name starting-position length)</pre>
</div>
<p>It can also be formatted as shown in this example:</p>
<pre>%SST(character-variable-name starting-position length)</pre>
<p>You can code *LDA in place of the character variable name to
indicate that the substring function is performed on the contents of the local
data area.</p>
<p>The substring function produces a substring from the contents
of the specified CL character variable or the local data area. The substring
begins at the specified starting position (which can be a variable name) and
continues for the length specified (which can also be a variable name). Neither
the starting position nor the length can be 0 or negative. If the sum of
the starting position and the length of the substring are greater than the
length of the entire variable or the local data area, an error occurs. The
length of the local data area is 1024.</p>
<p>The following are examples of
the substring built-in function: </p>
<ul><li>If the first two positions in the character variable &amp;NAME are IN,
the program INV210 is called. The entire value of &amp;NAME is passed to
INV210 and the value of &amp;ERRCODE is unchanged. Otherwise, the value of &amp;ERRCODE
is set to 99. <pre>DCL &amp;NAME *CHAR VALUE(INVOICE)
DCL &amp;ERRCODE *DEC (2 0)
IF (%SST(&amp;NAME 1 2) *EQ 'IN') +
THEN(CALL INV210 &amp;NAME)
ELSE CHGVAR &amp;ERRCODE 99</pre>
</li>
<li>If the first two positions of &amp;A match the first two positions of &amp;B,
the program CUS210 is called. <pre>DCL &amp;A *CHAR VALUE(ABC)
DCL &amp;B *CHAR VALUE(DEF)
IF (%SST(&amp;A 1 2) *EQ %SUBSTRING(&amp;B 1 2)) +
CALL CUS210</pre>
</li>
<li>Position and length can also be variables: This example changes the value
of &amp;X beginning at position &amp;Y for the length &amp;Z to 123. <pre>CHGVAR %SST(&amp;X &amp;Y &amp;Z) '123'</pre>
</li>
<li>If &amp;A is <samp class="codeph">ABCDEFG</samp> before this CHGVAR command is run, &amp;A
is <pre>CHGVAR %SST(&amp;A 2 3) '123'</pre>
<p><samp class="codeph">A123EFG</samp> after the command runs.</p>
</li>
<li>In this example, the length of the substring, 5, exceeds the length of
the operand YES to which it is compared. The operand is padded with blanks
so that the comparison is between YESNO and YESbb (where b is a blank). The
condition is false. <pre>DCL VAR(&amp;NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO)
.
.
.
IF (%SST (&amp;NAME 1 5) *EQ YES) +
THEN(CALL PROGA)</pre>
<div class="p">If the length of the substring is shorter than the operand,
the substring is padded with blanks for the comparison. For example: <pre>DCL VAR(&amp;NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO)
.
.
.
IF (%SST(&amp;NAME 1 3 ) *EQ YESNO) THEN(CALL PROG)</pre>
</div>
<p>This condition is false because YESbb (where bb is two
blanks) does not equal YESNO.</p>
</li>
<li>The value of the variable &amp;A is placed into positions 1 through 10
of the local data area. <pre>CHGVAR %SST(*LDA 1 10) &amp;A</pre>
</li>
<li>If the concatenation of positions 1 through 3 of the local data area plus
the constant 'XYZ' is equal to variable &amp;A, then PROCA is called. For
example, if positions 1 through 3 of the local data area contain 'ABC' and
variable &amp;A has a value of <samp class="codeph">ABCXYZ</samp>, the test is true and
PROCA is called. <pre>IF (((%SST*LDA 1 3) *CAT 'XYZ') *EQ &amp;A) THEN(CALLPRC PROCA)</pre>
</li>
<li>This procedure scans the character variable &amp;NUMBER and changes any
leading zeros to blanks. This can be used for simple editing of a field before
displaying in a message. <pre> DCL &amp;NUMBER *CHAR LEN(5)
DCL &amp;X *DEC LEN(3 0) VALUE(1)
.
.
LOOP:IF (%SST(&amp;NUMBER &amp;X 1) *EQ '0') DO
CHGVAR (%SST(&amp;NUMBER &amp;X 1)) ' ' /* Blank out */
CHGVAR &amp;X (&amp;X + 1) /* Increment */
IF (&amp;X *NE 4) GOTO LOOP
ENDDO</pre>
</li>
</ul>
<p>The following procedure uses the substring built-in function to find
the first sentence in a 50-character field &amp;INPUT and to place any remaining
text in a field &amp;REMAINDER It assumes that a sentence must have at least
2 characters, and no embedded periods.</p>
<pre> PGM (&amp;INPUT &amp;REMAINDER) /* SEARCH */
DCL &amp;INPUT *CHAR LEN(50)
DCL &amp;REMAINDER *CHAR LEN(50)
DCL &amp;X *INT /* INDEX */
DCL &amp;L *INT /* REMAINING LENGTH */
DOFORL:
DOFOR &amp;X 3 50
IF (%SST(&amp;INPUT &amp;X 1) *EQ '.') THEN(DO)
CHGVAR &amp;L (50-&amp;X)
CHGVAR &amp;X (&amp;X+1)
CHGVAR &amp;REMAINDER %SST(&amp;INPUT &amp;X &amp;L)
LEAVE
ENDDO
ENDDO
ENDPGM </pre>
<p>The procedure starts by checking the third position for a period.
Note that the substring function checks &amp;INPUT from position 3 to a length
of 1, which is position 3 only (length cannot be zero). If position 3 is
a period, the remaining length of &amp;INPUT is calculated. The value of &amp;X
is advanced to the beginning of the remainder, and the remaining portion of &amp;INPUT
is moved to &amp;REMAINDER.</p>
<p>If position 3 is not a period, the procedure
checks to see if it is at position 49. If so, it assumes that position 50
is a period and returns. If it is not at position 49, the procedure advances &amp;X
to position 4 and repeats the process.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="contp.htm" title="You can use commands to change the flow of logic within your CL procedure.">Control processing within a CL procedure</a></div>
</div>
<div class="reltasks"><strong>Related tasks</strong><br />
<div><a href="usvar.htm" title="Variables can be used to specify a list or qualified name.">Specify a list or qualified name using a variable</a></div>
<div><a href="valuv.htm" title="You can change the value of a CL variable using the Change Variable (CHGVAR) command.">Change the value of a variable</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="rbam6builtinfunc.htm" title="CL provides several built-in functions.">Built-in functions for CL</a></div>
</div>
<div class="relinfo"><strong>Related information</strong><br />
<div><a href="../clfinder/finder.htm">CL command finder</a></div>
<div><a href="../cl/if.htm">If (IF) command</a></div>
<div><a href="../cl/chgvar.htm">Change Variable (CHGVAR) command</a></div>
</div>
</div>
</body>
</html>