CHANGESTR.REX: Difference between revisions

From Rosetta Code
Content added Content deleted
(added a sample REXX code for the CHANGESTR subroutine. -- ~~~~)
 
m (correct spelling(s))
Line 4: Line 4:
║ original string (haystack)────────┐ │ │ [default: ≈ one billian]║
║ original string (haystack)────────┐ │ │ [default: ≈ one billian]║
║ old string to be changed───┐ │ │ │ ┌───begin at this occurance #.║
║ old string to be changed───┐ │ │ │ ┌───begin at this occurance #.║
║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrance]║
║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrence]║
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝
╚════════════════════════════╝ ↓ ↓ ↓ ↓ ↓ ╚═══════════════════════╝*/
╚════════════════════════════╝ ↓ ↓ ↓ ↓ ↓ ╚═══════════════════════╝*/
Line 13: Line 13:
w=length(o) /*length of OLD string.*/
w=length(o) /*length of OLD string.*/
if w==0 & t\=0 then return n || h /*changing a null char ? */
if w==0 & t\=0 then return n || h /*changing a null char ? */
#=0 /*# of changed occurances*/
#=0 /*# of changed occurrences*/
do j=1 until # >= t /*keep changing, T times.*/
do j=1 until # >= t /*keep changing, T times.*/
parse var h y (o) _ +(w) h /*parse the string ... */
parse var h y (o) _ +(w) h /*parse the string ... */
Line 20: Line 20:
else do
else do
$=$ || y || n /*build new STR from S. */
$=$ || y || n /*build new STR from S. */
#=#+1 /*bump occurance number. */
#=#+1 /*bump occurrence number.*/
end
end
end /*j*/
end /*j*/

Revision as of 12:08, 2 August 2013

<lang rexx>/*╔══════════════════════════════╗ CHANGESTR ╔═════════════════════════╗ ╔═╩══════════════════════════════╝ function ╚═════════════════════════╩═╗ ║ new string to be used──────────┐ ┌─────limit of # changes (times)║ ║ original string (haystack)────────┐ │ │ [default: ≈ one billian]║ ║ old string to be changed───┐ │ │ │ ┌───begin at this occurance #.║ ║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrence]║ ╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝

 ╚════════════════════════════╝   ↓ ↓ ↓ ↓ ↓   ╚═══════════════════════╝*/

changestr: procedure; parse arg o,h,n,t,b /* T and B are optional.*/ $= /*$: the returned string.*/ t=word(t 999999999 , 1) /*maybe use the default? */ b=word(b 1 , 1) /* " " " " */ w=length(o) /*length of OLD string.*/ if w==0 & t\=0 then return n || h /*changing a null char ? */

  1. =0 /*# of changed occurrences*/
               do j=1  until # >= t           /*keep changing, T times.*/
               parse var  h y  (o)  _  +(w) h /*parse the string ...   */
               if _== then return $ || y    /*no more left, return.  */
               if j<b   then $=$ || y || o    /*didn't meet begin at ? */
                        else do
                             $=$ || y || n    /*build new STR from S.  */
                             #=#+1            /*bump occurrence number.*/
                             end
               end   /*j*/
                                              /*Most REXX BIFs only ···*/

return $ || h /* support three options.*/</lang>