Difference between revisions of "In String"

From MidrangeWiki
Jump to: navigation, search
(Summary)
(Summary)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Sample Code]]
 
[[Category:Service Procedures]]
 
[[Category:Service Procedures]]
 +
__FORCETOC__
 
== 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 determine if a needle is found in a haystack—or not.  Optionally, the position of the needle in the haystack is also returned. See also [[In List]] and [[Get List Entry]],
+
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 determine if a needle is found in a haystack—or not.  Optionally, the position of the needle in the haystack is also returned.
  
== Service Procedure ==
+
See also [[In List]] and [[Get List Entry]].
 +
 
 +
By [[User:DaveLClarkI|Dave Clark]]
 +
 
 +
== Justification ==
 +
In addition to such excellent reasons for having service procedures as shareability and standardization of business rules, sometimes it is just a matter of wanting to make it clearer what your program logic is accomplishing—without necessarily having to resort to comments.
 +
{| class="wikitable"
 +
|+ style="padding-left:6px;text-align:left;"|Rule of Thumb
 +
|-
 +
|Sometimes, having a service procedure is simply a means of making code more self-documenting (i.e., easier for others to understand).
 +
|}
 +
To wit...  Which would you rather see in an RPG program—this?
 +
<pre>
 +
if %scan('TEMP':myString) > *zero; // if in string
 +
  // do something
 +
endif;
 +
</pre>
 +
...or, this?
 +
<pre>
 +
if GenUtl_inString('TEMP':myString);
 +
  // do something
 +
endif;
 +
</pre>
 +
It is the opinion of many that the second form is self-documenting and is therefore more readable.  Hence, the reason for having such a small service procedure.
 +
 
 +
== 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 17: Line 45:
 
   pIndex                packed(5:0)    options(*nopass);
 
   pIndex                packed(5:0)    options(*nopass);
 
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 34: Line 70:
 
   iPosn = %scan(pNeedle: pHaystack);  // look for needle in haystack
 
   iPosn = %scan(pNeedle: pHaystack);  // look for needle in haystack
  
   if %parms < %parmnum(pIndex)         // if optional parm not passed
+
   if %parms() < %parmnum(pIndex)       // if optional parm not passed
 
   or %addr(pIndex) = *null;            // or optional parm was omitted
 
   or %addr(pIndex) = *null;            // or optional parm was omitted
 
   else;                                // skip it, else
 
   else;                                // skip it, else

Latest revision as of 16:40, 17 December 2018


Summary

The following are the RPG/LE fully free-form definitions and instructions needed for using the In String service procedure. This service procedure simply allows the caller to determine if a needle is found in a haystack—or not. Optionally, the position of the needle in the haystack is also returned.

See also In List and Get List Entry.

By Dave Clark

Justification

In addition to such excellent reasons for having service procedures as shareability and standardization of business rules, sometimes it is just a matter of wanting to make it clearer what your program logic is accomplishing—without necessarily having to resort to comments.

Rule of Thumb
Sometimes, having a service procedure is simply a means of making code more self-documenting (i.e., easier for others to understand).

To wit... Which would you rather see in an RPG program—this?

if %scan('TEMP':myString) > *zero; // if in string
  // do something
endif;

...or, this?

if GenUtl_inString('TEMP':myString);
  // do something
endif;

It is the opinion of many that the second form is self-documenting and is therefore more readable. Hence, the reason for having such a small service procedure.

Service Prototype

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

**free

//==============================================================================
// This procedure determines if a needle is found in a haystack--or not.  The
// haystack is represented by a long character string.  Optionally, the
// beginning character index position of the needle is also returned.
//==============================================================================
dcl-pr GenUtl_InString  ind;
  pNeedle               varchar(32767) const;
  pHayStack             varchar(65535) const;
  pIndex                packed(5:0)    options(*nopass);
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);

//==============================================================================
// This procedure determines if a needle is found in a haystack--or not.  The
// haystack is represented by a long character string.  Optionally, the
// beginning character index position of the needle is also returned.
//==============================================================================
dcl-proc GenUtl_InString     export;
  dcl-pi *n             ind;
    pNeedle             varchar(32767) const;
    pHayStack           varchar(65535) const;
    pIndex              packed(5:0)    options(*nopass);
  end-pi;

  dcl-s iPosn         packed(5:0);                                       

  iPosn = %scan(pNeedle: pHaystack);   // look for needle in haystack

  if %parms() < %parmnum(pIndex)       // if optional parm not passed
  or %addr(pIndex) = *null;            // or optional parm was omitted
  else;                                // skip it, else
    pIndex = iPosn;                    // return position of needle in haystack
  endif;

  return (iPosn > *zero);              // indicate if found or not
end-proc;