Difference between revisions of "Scan and Replace"

From MidrangeWiki
Jump to: navigation, search
(Summary)
(Summary)
Line 1: Line 1:
 
[[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.  If the replacement "straw" is omitted (or a zero-length string) then the "needle" is deleted by default.
+
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 omitted (or is a zero-length string) then the "needle" is deleted by default.
  
 
== Service Procedure ==
 
== Service Procedure ==

Revision as of 20:33, 12 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 omitted (or is a zero-length string) then the "needle" is deleted by default.

Service Procedure

**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;

//==============================================================================
// 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)); // remove it
    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;