178 lines
9.8 KiB
HTML
178 lines
9.8 KiB
HTML
|
<?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 &NAME are IN,
|
||
|
the program INV210 is called. The entire value of &NAME is passed to
|
||
|
INV210 and the value of &ERRCODE is unchanged. Otherwise, the value of &ERRCODE
|
||
|
is set to 99. <pre>DCL &NAME *CHAR VALUE(INVOICE)
|
||
|
DCL &ERRCODE *DEC (2 0)
|
||
|
IF (%SST(&NAME 1 2) *EQ 'IN') +
|
||
|
THEN(CALL INV210 &NAME)
|
||
|
ELSE CHGVAR &ERRCODE 99</pre>
|
||
|
</li>
|
||
|
<li>If the first two positions of &A match the first two positions of &B,
|
||
|
the program CUS210 is called. <pre>DCL &A *CHAR VALUE(ABC)
|
||
|
DCL &B *CHAR VALUE(DEF)
|
||
|
IF (%SST(&A 1 2) *EQ %SUBSTRING(&B 1 2)) +
|
||
|
CALL CUS210</pre>
|
||
|
</li>
|
||
|
<li>Position and length can also be variables: This example changes the value
|
||
|
of &X beginning at position &Y for the length &Z to 123. <pre>CHGVAR %SST(&X &Y &Z) '123'</pre>
|
||
|
</li>
|
||
|
<li>If &A is <samp class="codeph">ABCDEFG</samp> before this CHGVAR command is run, &A
|
||
|
is <pre>CHGVAR %SST(&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(&NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO)
|
||
|
.
|
||
|
.
|
||
|
.
|
||
|
IF (%SST (&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(&NAME) TYPE(*CHAR) LEN(5) VALUE(YESNO)
|
||
|
.
|
||
|
.
|
||
|
.
|
||
|
IF (%SST(&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 &A is placed into positions 1 through 10
|
||
|
of the local data area. <pre>CHGVAR %SST(*LDA 1 10) &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 &A, then PROCA is called. For
|
||
|
example, if positions 1 through 3 of the local data area contain 'ABC' and
|
||
|
variable &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 &A) THEN(CALLPRC PROCA)</pre>
|
||
|
</li>
|
||
|
<li>This procedure scans the character variable &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 &NUMBER *CHAR LEN(5)
|
||
|
DCL &X *DEC LEN(3 0) VALUE(1)
|
||
|
.
|
||
|
.
|
||
|
LOOP:IF (%SST(&NUMBER &X 1) *EQ '0') DO
|
||
|
CHGVAR (%SST(&NUMBER &X 1)) ' ' /* Blank out */
|
||
|
CHGVAR &X (&X + 1) /* Increment */
|
||
|
IF (&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 &INPUT and to place any remaining
|
||
|
text in a field &REMAINDER It assumes that a sentence must have at least
|
||
|
2 characters, and no embedded periods.</p>
|
||
|
<pre> PGM (&INPUT &REMAINDER) /* SEARCH */
|
||
|
DCL &INPUT *CHAR LEN(50)
|
||
|
DCL &REMAINDER *CHAR LEN(50)
|
||
|
DCL &X *INT /* INDEX */
|
||
|
DCL &L *INT /* REMAINING LENGTH */
|
||
|
|
||
|
DOFORL:
|
||
|
DOFOR &X 3 50
|
||
|
IF (%SST(&INPUT &X 1) *EQ '.') THEN(DO)
|
||
|
CHGVAR &L (50-&X)
|
||
|
CHGVAR &X (&X+1)
|
||
|
CHGVAR &REMAINDER %SST(&INPUT &X &L)
|
||
|
LEAVE
|
||
|
ENDDO
|
||
|
ENDDO
|
||
|
ENDPGM </pre>
|
||
|
<p>The procedure starts by checking the third position for a period.
|
||
|
Note that the substring function checks &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 &INPUT is calculated. The value of &X
|
||
|
is advanced to the beginning of the remainder, and the remaining portion of &INPUT
|
||
|
is moved to &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 &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>
|