Difference between revisions of "Get Scan Count"

From MidrangeWiki
Jump to: navigation, search
(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...")
 
Line 5: Line 5:
  
 
== Service Prototype ==
 
== Service Prototype ==
Place the following in a separate copybook for inclusion in the source of both the caller and the service program members.
+
Place the following in a separate copybook for inclusion in both the caller and the service program source members.
 
<pre>
 
<pre>
 
**free
 
**free
Line 19: Line 19:
  
 
== Service Procedure ==
 
== Service Procedure ==
Place the following in the source of a service program member.
+
Place the following in a service program source member.
 
<pre>
 
<pre>
 
//******************************************************************************
 
//******************************************************************************

Revision as of 15:56, 14 December 2018

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 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.

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