Get Scan Count
From MidrangeWiki
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.
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 //****************************************************************************** // 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 a service program source member.
**free ctl-opt NoMain AlwNull(*UsrCtl) Debug Option(*SrcStmt:*NoDebugIo) DatFmt(*ISO) TimFmt(*ISO); //****************************************************************************** // 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;