Difference between revisions of "Scan and Replace"

From MidrangeWiki
Jump to: navigation, search
(Created page with "Category:Service Procedures == Summary == The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure. This se...")
 
(Examples)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Sample Code]]
 
[[Category:Service Procedures]]
 
[[Category:Service Procedures]]
 
== Summary ==
 
== Summary ==
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to replace (or delete) all needles in a haystack.
+
The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} service procedure.  This service procedure simply allows the caller to find all needles in a haystack and replace them with a straw.  If the replacement "straw" is a zero-length string, then the "needle" is simply deleted.
  
== Service Procedure ==
+
By [[User:DaveLClarkI|Dave Clark]]
 +
 
 +
== Service Prototype ==
 +
Place the following in a separate copybook for inclusion in both the caller and the service program source members.
 
<pre>
 
<pre>
 
**free
 
**free
Line 16: Line 20:
 
   Straw                varchar(32767) const;
 
   Straw                varchar(32767) const;
 
end-pr;
 
end-pr;
 +
</pre>
 +
 +
== Service Procedure ==
 +
Place the following in a service program source member.
 +
<pre>
 +
**free
 +
ctl-opt NoMain AlwNull(*UsrCtl) Debug Option(*SrcStmt:*NoDebugIo)
 +
        DatFmt(*ISO) TimFmt(*ISO);
  
 
//==============================================================================
 
//==============================================================================
Line 35: Line 47:
  
 
   dow posn > *zero;                    // loop on needles found
 
   dow posn > *zero;                    // loop on needles found
     NewHaystack = %replace(Straw: NewHaystack: posn: %len(Needle)); // remove it
+
     NewHaystack = %replace(Straw: NewHaystack: posn: %len(Needle));
 
     if ((posn + %len(Straw)) <= %len(NewHaystack)); // room for more needles?
 
     if ((posn + %len(Straw)) <= %len(NewHaystack)); // room for more needles?
 
       posn = %scan(Needle: NewHaystack: posn + %len(Straw)); // yes, find one
 
       posn = %scan(Needle: NewHaystack: posn + %len(Straw)); // yes, find one
Line 46: Line 58:
 
end-proc;
 
end-proc;
 
</pre>
 
</pre>
 +
 +
== Examples ==
 +
See the [[Change Current Library]] service procedure.
 +
 +
See the [[Make Quoted String]] service procedure.
 +
 +
See the [[Strip Formatting]] service procedure.

Latest revision as of 17:46, 17 December 2018

Summary

The following are the RPG/LE fully free-form definitions and instructions needed for using the Scan and Replace service procedure. This service procedure simply allows the caller to find all needles in a haystack and replace them with a straw. If the replacement "straw" is a zero-length string, then the "needle" is simply deleted.

By Dave Clark

Service Prototype

Place the following in a separate copybook for inclusion in both the caller and the service program source members.

**free

//==============================================================================
// Find all occurrences of a "needle" in the "haystack" and replace them
// with the "straw" -- returning the "newhaystack".
//==============================================================================
dcl-pr GenUtl_ScanAndReplace      varchar(65535) rtnparm;
  Needle                varchar(32767) const;
  Haystack              varchar(65535) const;
  Straw                 varchar(32767) const;
end-pr;

Service Procedure

Place the following in a service program source member.

**free
ctl-opt NoMain AlwNull(*UsrCtl) Debug Option(*SrcStmt:*NoDebugIo)
        DatFmt(*ISO) TimFmt(*ISO);

//==============================================================================
// Find all occurrences of a "needle" in the "haystack" and replace them
// with the "straw" -- returning the "newhaystack".
//==============================================================================
dcl-proc GenUtl_ScanAndReplace    export;
  dcl-pi *n             varchar(65535) rtnparm;
    Needle              varchar(32767) const;
    Haystack            varchar(65535) const;
    Straw               varchar(32767) const;
  end-pi;

  dcl-s NewHaystack     like(Haystack);
  dcl-s posn            packed(5:0);

  NewHaystack = Haystack;              // transfer haystack to internal area
  posn = %scan(Needle: NewHaystack);   // find first needle, if any

  dow posn > *zero;                    // loop on needles found
    NewHaystack = %replace(Straw: NewHaystack: posn: %len(Needle));
    if ((posn + %len(Straw)) <= %len(NewHaystack)); // room for more needles?
      posn = %scan(Needle: NewHaystack: posn + %len(Straw)); // yes, find one
    else;                              // else
      posn = *zero;                    // end of haystack
    endif;
  enddo;                               // end loop on needles found

  return NewHaystack;                  // return to caller
end-proc;

Examples

See the Change Current Library service procedure.

See the Make Quoted String service procedure.

See the Strip Formatting service procedure.