Get Scan Count
From MidrangeWiki
Revision as of 15:54, 14 December 2018 by DaveLClarkI (talk | contribs) (Created page with "Category:Sample Code Category:Service Procedures == Summary == The following are the RPG/LE fully free-form definitions and instructions needed for using the {{AN}} se...")
Summary
The following are the RPG/LE fully free-form definitions and instructions needed for using the Get Scan Count service procedure. This service procedure simply allows the caller to retrieve a count of the number of times a needle is found in a haystack.
Service Prototype
Place the following in a separate copybook for inclusion in the source of both the caller and the service program members.
**free //****************************************************************************** // Get a count of the number of times needle was found in haystack. //****************************************************************************** dcl-pr GenUtl_GetScanCount int(10); pNeedle varchar(256) value; pHaystack varchar(65535) value; end-pr;
Service Procedure
Place the following in the source of a service program member.
//****************************************************************************** // Get a count of the number of times needle was found in haystack. //****************************************************************************** dcl-proc GenUtl_GetScanCount export; dcl-pi *n int(10); pNeedle varchar(256) value; pHaystack varchar(65535) value; end-pi; dcl-s Posn packed(5:0); dcl-s haystackLength packed(5:0); dcl-s scanCount int(10); scanCount = *zero; // initialize counter haystackLength = %len(pHaystack); // one-time retrieval of length Posn = %scan(pNeedle: pHaystack); // find first occurrence dow Posn > *zero // loop on needles and Posn <= haystackLength; // within the haystack scanCount += 1; // count it if (Posn + %len(pNeedle)) <= haystackLength; // beyond end of haystack? Posn = %scan(pNeedle: pHaystack: Posn + %len(pNeedle)); // no, search again else; // else Posn = *zero; // needle can't fit in remaining haystack endif; enddo; // end of loop on needles return scanCount; // return scan count end-proc;