CHANGESTR.REX: Difference between revisions

Content added Content deleted
(added functionality to this version of the CHANGESTR function, changed comments, indentation. -- ~~~~)
m (updated some comments in this version of the BIF. -- ~~~~)
Line 1: Line 1:
This version of the   '''changestr'''   BIF has more functionality than the standard BIF.

<lang rexx>/*REXX program emulates the CHANGESTR built-in function for older REXXes*/
<lang rexx>/*REXX program emulates the CHANGESTR built-in function for older REXXes*/
/*──── This version has more functionality: limit the number of changes.*/
/*──── This version has more functionality: limit the number of changes.*/
/*──── start of change occurance. */
/*──── start of change occurrence#.*/
/*──── start of change position. */
/*──── start of change position. */


/*╔══════════════════════════ CHANGESTR function ══════════════════════╗
/*╔══════════════════════════ CHANGESTR function ══════════════════════╗
╔═╩════════════════════════════════════════════════════════════════════╩═╗
╔═╩════════════════════════════════════════════════════════════════════╩═╗
║ The CHANGESTR function is used to replace some or all occurances of an ║
║ The CHANGESTR function is used to replace some or all occurrences of an║
║ (old) string in a haystack with a new string. The changed string is ║
║ (old) string in a haystack with a new string. The changed string is ║
║ returned. If the haystack doesn't contain the old string, the ║
║ returned. If the haystack doesn't contain the old string, the ║
Line 13: Line 15:
║ ║
║ ║
║ new string to be used►──────────┐ ┌─────◄limit of # changes (times).║
║ new string to be used►──────────┐ ┌─────◄limit of # changes (times).║
║ original string (haystack)►──────┐ │ │ [default: ≈ one billian]║
║ original string (haystack)►──────┐ │ │ [default: ≈ one billion]║
║ old string to be replaced►──┐ │ │ │ ┌────◄begin at this occurance #.
║ old string to be replaced►──┐ │ │ │ ┌────◄begin at this occurrence #║
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄start position (default=1)║
║ {O, H, and N can be null.} │ │ │ │ │ ┌──◄start position (default=1)║
╚═╦════════════════════════════╗ │ │ │ │ │ │ ╔═════════════════════════╦═╝
╚═╦════════════════════════════╗ │ │ │ │ │ │ ╔═════════════════════════╦═╝
Line 40: Line 42:
h=substr(h,p) /*only use this part of H.*/
h=substr(h,p) /*only use this part of H.*/
end /*now, proceed as usual. */
end /*now, proceed as usual. */
#=0 /*# of changed occurances.*/
#=0 /*# of changed occurrences*/
do j=1 while # < t /*keep changing, T times. */
do j=1 while # < t /*keep changing, T times. */
parse var h y (o) _ +(L) h /*parse the haystack ··· */
parse var h y (o) _ +(L) h /*parse the haystack ··· */
Line 46: Line 48:
$=$ || y /*append the residual txt.*/
$=$ || y /*append the residual txt.*/
if j<b then $=$ || o /*append OLD if too soon. */
if j<b then $=$ || o /*append OLD if too soon. */
else do /*met the occurance test. */
else do /*met the occurrence test.*/
$=$ || n /*append the NEW string.*/
$=$ || n /*append the NEW string.*/
#=#+1 /*bump occurance number.*/
#=#+1 /*bump occurrence number.*/
end
end
end /*j*/
end /*j*/ /*Note: most REXX ··· */
/*Note: Most REXX BIFs··· */
/* CHANGESTR BIFs only ···*/
return f || $ || h /* only support 3 options.*/</lang>
return f || $ || h /* support three options. */</lang>