midrange.com code scratchpad
Name:
Anonymous
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
12/29/2011 06:54:44 pm
IP:
Logged
Description:
Service Program COUNTWORDS.

(See inline documentation)
Code:
  1.      H/Title Service program for text processing                                                            
  2.      H OPTION(*SRCSTMT: *NODEBUGIO: *SHOWCPY :*XREF)                                                        
  3.      H DEBUG(*YES)                                                                                          
  4.                                                                                                             
  5.         //======================================================================                            
  6.         // Author:  Steve Landess                                                                           
  7.         //   Date:  12/19/2011                                                                              
  8.         //======================================================================                            
  9.         //                                                                                                  
  10.         // In subprocedure prCntWords, for the purposes of counting words,                                  
  11.         // a "word" is a value which is delimited by spaces.                                                
  12.         //                                                                                                  
  13.         // Since spaces are considered delimiters, all "words" will be                                      
  14.         // left-justified into the Words array.  Extra spaces between words                                 
  15.         // are ignored.                                                                                     
  16.         //                                                                                                  
  17.         //  Examples:                                                                                       
  18.         //                                                                                                  
  19.         //   ' and/or ' contains one word.                                                                  
  20.         //   ' A and/or B ' contains three words.                                                           
  21.         //   ' A and/ or B ' contains four words.                                                           
  22.         //   ' A and / or B ' contains five words.                                                          
  23.         //                                                                                                  
  24.         //======================================================================                            
  25.         // To recreate the Service program COUNTWORDS:                                                      
  26.         //                                                                                                  
  27.         //  1) DLTSRVPGM MYOBJLIB/COUNTWORDS                                                                 
  28.         //  2) DLTMOD    MYOBJLIB/COUNTWORDS                                                                 
  29.         //  3) CRTRPGMOD MODULE(MYOBJLIB/COUNTWORDS) SRCFILE(MYOBJLIB/QRPGLESRC) DBGVIEW(*ALL)                
  30.         //  4) CRTSRVPGM SRVPGM(MYOBJLIB/COUNTWORDS) EXPORT(*ALL)                                            
  31.         //                                                                                                  
  32.         //======================================================================                            
  33.         // To recreate the SQL UDF (User-Defined Function) COUNTWORDS,                                      
  34.         // use RUNSQLSTM or execute the following SQL commands via STRSQL:                                  
  35.         //                                                                                                  
  36.         //  1) DROP FUNCTION QGPL/COUNTWORDS ( VARCHAR(180))                                                
  37.         //                                                                                                  
  38.         //  2) CREATE FUNCTION QGPL/COUNTWORDS ( VARCHAR(180) )                                             
  39.         //     RETURNS NUMERIC(8,0)                                                                         
  40.         //     EXTERNAL NAME 'MYOBJLIB/COUNTWORDS(PRCNTWORDS)'                                               
  41.         //     LANGUAGE RPGLE                                                                               
  42.         //     NO SQL                                                                                       
  43.         //     NOT DETERMINISTIC                                                                            
  44.         //     NOT FENCED                                                                                   
  45.         //     RETURNS NULL ON NULL INPUT                                                                   
  46.         //                                                                                                  
  47.         //--------------------------                                                                        
  48.         // Example of usage:                                                                                
  49.         //                                                                                                  
  50.         //     Select                                                                                       
  51.         //       countwords( FULL_NAME ) as NbrWords                                                        
  52.         //     , FULL_NAME                                                                                  
  53.         //     From addresses                                                                                
  54.         //                                                                                                  
  55.         //  Output:                                                                                         
  56.         //                                                                                                  
  57.         // NBRWORDS   FULL NAME                                                                             
  58.         // ---------  --------------------------------------------------------------                        
  59.         //        3   Compass Bank ISAOA/ATIMA                                                              
  60.         //        6   Great Western Financial Services Inc ISAOA/ATIMA                                      
  61.         //        5   Chase Home Finance LLC ISAOA                                                          
  62.         //        9   Wells Fargo Bank NA #xxx its Successors and/or Assigns                                
  63.         //        3   Central Mortgage Company                                                              
  64.         //        7   Flagstar Bank FSB Its Successors and/or Assigns                                       
  65.         //       10   BAC Home Loans Servicing, LP ISAOA ATIMA Insurance Department TXxxxxxxxxxxx           
  66.         //                                                                                                  
  67.         //======================================================================                            
  68.         // To recreate the SQL UDF (User-Defined Function) DUPEWORDS,                                       
  69.         // use RUNSQLSTM or execute the following SQL commands via STRSQL:                                  
  70.         //                                                                                                  
  71.         //  1) DROP FUNCTION QGPL/DUPEWORDS ( VARCHAR(180))                                                 
  72.         //                                                                                                  
  73.         //  2) CREATE FUNCTION QGPL/DUPEWORDS ( VARCHAR(180) )                                              
  74.         //     RETURNS NUMERIC(8,0)                                                                         
  75.         //     EXTERNAL NAME 'MYOBJLIB/COUNTWORDS(PRDUPWORDS)'                                               
  76.         //     LANGUAGE RPGLE                                                                               
  77.         //     NO SQL                                                                                       
  78.         //     NOT DETERMINISTIC                                                                            
  79.         //     NOT FENCED                                                                                   
  80.         //     RETURNS NULL ON NULL INPUT                                                                   
  81.         //                                                                                                  
  82.         //--------------------------                                                                        
  83.         // Example of usage:                                                                                
  84.         //                                                                                                  
  85.         //     Select                                                                                       
  86.         //        dupewords( full_name )                                                                    
  87.         //      , substr( full_name,1,90 )  as full_name                                                    
  88.         //      , substr( lastname,1,90 )  as lastname                                                      
  89.         //      , groupcode                                                                                 
  90.         //      , entitycode                                                                                
  91.         //     From addresses                                                                                
  92.         //     Where groupcode = 'INSURED'                                                                  
  93.         //       and lastname is not null                                                                   
  94.         //       and dupewords( full_name ) > 1                                                             
  95.         //                                                                                                  
  96.         //  Output:                                                                                         
  97.         //                                                                                                  
  98.         //    DUPEWORDS  FULL_NAME                                                                          
  99.         //    ---------  --------------------------------------------------------------                     
  100.         //           2   Xxxxxx E Xxxxxx Bbbbbb & Carolyn Bbbbbb                                            
  101.         //           3   Rrrrrrr E Rrrrrrr E Brrrrrr & Rebecca L Brrrrrr                                    
  102.         //           2   Larry W Larry Wwwww Oooooooooo & Melissa J Oooooooooo                              
  103.         //                                                                                                  
  104.         //======================================================================                            
  105.         // To recreate the SQL UDF (User-Defined Function) NEXTWORD,                                        
  106.         // use RUNSQLSTM or execute the following SQL commands via STRSQL:                                  
  107.         //                                                                                                  
  108.         //  1) DROP FUNCTION QGPL/NEXTWORD ( VARCHAR(60) , VARCHAR(180) )                                   
  109.         //                                                                                                  
  110.         //  2) CREATE FUNCTION QGPL/NEXTWORD (VARCHAR(60) , VARCHAR(180) )                                  
  111.         //     RETURNS CHAR(60)                                                                             
  112.         //     EXTERNAL NAME 'MYOBJLIB/COUNTWORDS(PRNXTWORD)'                                                
  113.         //     LANGUAGE RPGLE                                                                               
  114.         //     NO SQL                                                                                       
  115.         //     NOT DETERMINISTIC                                                                            
  116.         //     NOT FENCED                                                                                   
  117.         //     RETURNS NULL ON NULL INPUT                                                                   
  118.         //                                                                                                  
  119.         //--------------------------                                                                        
  120.         // Example of usage:                                                                                
  121.         //                                                                                                  
  122.         //     Select                                                                                       
  123.         //       substr( nextword( 'Box',address1 ),1,10 ) as nextword                                      
  124.         //     , address1                                                                                   
  125.         //      from addresses                                                                               
  126.         //     where upper( address1 ) like '%BOX%'                                                         
  127.         //                                                                                                  
  128.         //  Output:                                                                                         
  129.         //                                                                                                  
  130.         //   NEXTWORD    ADDRESS1                                                                           
  131.         //    ---------  --------------------------------------------------------------                     
  132.         //   200017      PO Box 200017                                                                      
  133.         //   47020       PO Box 47020                                                                       
  134.         //   23000       PO Box 23000                                                                       
  135.         //   7026        PO Box 7026                                                                        
  136.         //   961206      PO Box 961206                                                                      
  137.         //   1367        PO Box 1367                                                                        
  138.         //   5708        PO Box 5708                                                                        
  139.         //   961291      P.O. Box 961291                                                                    
  140.         //                                                                                                  
  141.         //======================================================================                            
  142.                                                                                                             
  143.        //--------------------------------------                                                             
  144.        // Global variables                                                                                  
  145.        //--------------------------------------                                                             
  146.                                                                                                             
  147.      d Blank           s              1A   Inz(*blanks)                                                     
  148.      d Word            s             60A   Inz(*blanks)                                                     
  149.      d WordVar         s             60A   Inz(*blanks) varying                                             
  150.      d NameVar         s            180A   varying                                                          
  151.      d NbrWords        s              8s 0                                                                  
  152.      d NbrDupes        s              8s 0                                                                  
  153.                                                                                                             
  154.      d WordsDS         ds                                                                                   
  155.                                                                                                             
  156.      d Words                               Like(WordVar)                                                    
  157.      d                                     Dim(50)                                                          
  158.      d                                     Inz(*HIVAL)                                                      
  159.                                                                                                             
  160.      D  WordsAscend                        Like(WordVar)                                                    
  161.      d                                     Dim(%elem(Words))                                                
  162.      d                                     Overlay(WordsDS)                                                 
  163.      D                                     Ascend                                                           
  164.                                                                                                             
  165.        // Constants                                                                                         
  166.                                                                                                             
  167.      D lo              C                   'abcdefghijklmnopqrstuvwxyz'         Lower Case alphabet         
  168.      D up              C                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'         Lower Case alphabet         
  169.                                                                                                             
  170.        //======================================                                                             
  171.        // Prototypes                                                                                        
  172.        //======================================                                                             
  173.                                                                                                             
  174.      d prCntWords...                                                                                        
  175.      d                 pr                  Like(NbrWords)                                                   
  176.      D  pName                              Like(NameVar)                                                    
  177.                                                                                                             
  178.      d prDupWords...                                                                                        
  179.      d                 pr                  Like(NbrWords)                                                   
  180.      D  pName                              Like(NameVar)                                                    
  181.                                                                                                             
  182.      d prNxtWord...                                                                                         
  183.      d                 pr                  Like(Word)                                                       
  184.      D  pFindWord                          Like(WordVar)                                                    
  185.      D  pName                              Like(NameVar)                                                    
  186.                                                                                                             
  187.        //======================================                                                             
  188.        // Subprocedures                                                                                     
  189.        //======================================                                                             
  190.                                                                                                             
  191.        //--------------------------------------                                                             
  192.        // Count number of words in string.                                                                  
  193.        //--------------------------------------                                                             
  194.                                                                                                             
  195.      p prCntWords...                                                                                        
  196.      p                 b                   export                                                           
  197.      d prCntWords...                                                                                        
  198.      d                 pi                  Like(NbrWords)                                                   
  199.      D  pName                              Like(NameVar)                                                    
  200.                                                                                                             
  201.      D NameDs          DS           180    Inz                                                              
  202.      D   Char                         1    Dim(180) Overlay(NameDS)                                         
  203.                                                                                                             
  204.      D Pos             s             10i 0 Inz(*zeros)                                                      
  205.      D EndPos          s             10i 0 Inz(*zeros)                                                      
  206.      D Index1          s             10i 0 Inz(*zeros)                          Index for Word field        
  207.      D Index2          s             10i 0 Inz(*zeros)                          Index for Words arry        
  208.      D Index3          s             10i 0 Inz(*zeros)                          Index for Words arry        
  209.      D Index4          s             10i 0 Inz(*zeros)                          Index for                   
  210.      D Index5          s             10i 0 Inz(*zeros)                          Index for                   
  211.                                                                                                             
  212.       /Free                                                                                                 
  213.                                                                                                             
  214.         Clear NbrWords;                                                                                     
  215.                                                                                                             
  216.         If pName = *blanks;                                                                                 
  217.            *InLR = *On;                                                                                     
  218.            Return NbrWords;                                                                                 
  219.         EndIf;                                                                                              
  220.                                                                                                             
  221.         Exsr SrWords1;                                                                                      
  222.         Return NbrWords;                                                                                    
  223.         *InLr = *Off;                                                                                       
  224.                                                                                                             
  225.        //--------------------------------------------------------------------                               
  226.        // srWords1 - Count number of words in string                                                        
  227.        //--------------------------------------------------------------------                               
  228.                                                                                                             
  229.        Begsr srWords1;                                                                                      
  230.                                                                                                             
  231.            Clear Word;                                                                                      
  232.            Reset Words;                                                                                     
  233.            Clear Index1;                                                                                    
  234.            Clear Index2;                                                                                    
  235.                                                                                                             
  236.            NameDS  =  %trim( pName );                                                                       
  237.            EndPos  = %checkr(Blank : NameDS);                                                               
  238.                                                                                                             
  239.            For Pos = 1 to EndPos;                                                                           
  240.                                                                                                             
  241.                If Char(Pos) <> *blanks;                            // Build up the word value               
  242.                   Index1 += 1;                                     //  into the field 'word'                
  243.                   %subst(Word: Index1 : 1) = Char(Pos);                                                     
  244.                EndIf;                                                                                       
  245.                                                                                                             
  246.                If (Word <> *blanks) and (Char(Pos) = *blanks)      // When a blank is encountered           
  247.                Or (Pos = EndPos);                                  // Or end of NameDS string is rea        
  248.                   Index2 += 1;                                     // Words array index                     
  249.                   Words(Index2) = Word;                            // Load Words Array                      
  250.                   Clear Word;                                                                               
  251.                   Index1 = *zeros;                                                                          
  252.                EndIf;                                                                                       
  253.                                                                                                             
  254.            EndFor;                                                                                          
  255.                                                                                                             
  256.            NbrWords = Index2;                                                                               
  257.                                                                                                             
  258.         Endsr;                                                                                              
  259.                                                                                                             
  260.       /End-Free                                                                                             
  261.                                                                                                             
  262.      P prCntWords...                                                                                        
  263.      P                 E                                                                                    
  264.                                                                                                             
  265.       //---------------------------------------------------                                                 
  266.       // Count number of duplicate words in a string                                                        
  267.       //---------------------------------------------------                                                 
  268.                                                                                                             
  269.      p prDupWords...                                                                                        
  270.      p                 b                   export                                                           
  271.      d prDupWords...                                                                                        
  272.      d                 pi                  Like(NbrWords)                                                   
  273.      D  pName                              Like(NameVar)                                                    
  274.                                                                                                             
  275.      D Index1          s             10i 0 Inz(*zeros)                                                      
  276.      D Index2          s             10i 0 Inz(*zeros)                                                      
  277.      D Index3          s             10i 0 Inz(*zeros)                                                      
  278.      D NbrWords        s              8S 0 Inz(*zeros)                                                      
  279.                                                                                                             
  280.      d DupWords        s                   Like(WordVar)                                                    
  281.      d                                     Dim(%Elem(Words))                                                
  282.      d                                     Inz(*HIVAL)                                                      
  283.      d                                     Ascend                                                           
  284.                                                                                                             
  285.       /Free                                                                                                 
  286.                                                                                                             
  287.         Reset Words;                                        // Global array Words                           
  288.         Reset Dupwords;                                     // Local array Dupewords                        
  289.         Clear NbrDupes;                                                                                     
  290.                                                                                                             
  291.         NbrWords = prCntWords(pName);                       // loads Words array                            
  292.         Index3 = %Elem(Words);                                                                              
  293.                                                                                                             
  294.         If NbrWords > 1;                                                                                    
  295.            SortA WordsAscend;                                                                               
  296.            For Index1 = 1 to NbrWords;                                                                      
  297.                Index2 = %lookup( WordsAscend( Index1 ) : DupWords );                                        
  298.                If Index2 <> *zeros;                                                                         
  299.                   NbrDupes += 1;                                                                            
  300.                Else;                                                                                        
  301.                   Index2 = %lookup ( *HIVAL : DupWords );                                                   
  302.                   If Index2 > *zeros;                                                                       
  303.                      DupWords ( Index2 ) = WordsAscend( Index1 ) ;                                          
  304.                   EndIf;                                                                                    
  305.                EndIf;                                                                                       
  306.            EndFor;                                                                                          
  307.         EndIf;                                                                                              
  308.                                                                                                             
  309.         Return NbrDupes;                                                                                    
  310.                                                                                                             
  311.       /End-Free                                                                                             
  312.                                                                                                             
  313.      P prDupWords...                                                                                        
  314.      P                 E                                                                                    
  315.                                                                                                             
  316.       //---------------------------------------------------                                                 
  317.       // Find and return next word                                                                          
  318.       //---------------------------------------------------                                                 
  319.                                                                                                             
  320.      p prNxtWord...                                                                                         
  321.      p                 b                   export                                                           
  322.      d prNxtWord...                                                                                         
  323.      d                 pi                  Like(Word)                                                       
  324.      D  pFindWord                          Like(WordVar)                                                    
  325.      D  pName                              Like(NameVar)                                                    
  326.                                                                                                             
  327.      D Index1          s             10i 0 Inz(*zeros)                                                      
  328.      D Index2          s             10i 0 Inz(*zeros)                                                      
  329.                                                                                                             
  330.      D NextWord        s             60A   Inz(*blanks)                                                     
  331.                                                                                                             
  332.       /Free                                                                                                 
  333.                                                                                                             
  334.         Reset Words;                                        // Global array Words                           
  335.         Clear NextWord;                                                                                     
  336.                                                                                                             
  337.         NbrWords = prCntWords(pName);                      // loads Words array                             
  338.                                                                                                             
  339.         // Cannot use %LOOKUP on unsequenced array...                                                       
  340.                                                                                                             
  341.         If NbrWords > *zeros;                                                                               
  342.            For Index1 = 1 to NbrWords;                                                                      
  343.                If pFindWord = Words( Index1 );                                                              
  344.                   If Index1 < NbrWords;                                                                     
  345.                      NextWord = Words( Index1 + 1 );                                                        
  346.                      Leave;                                                                                 
  347.                   EndIf;                                                                                    
  348.                EndIf;                                                                                       
  349.            EndFor;                                                                                          
  350.         EndIf;                                                                                              
  351.                                                                                                             
  352.         Return NextWord;                                                                                    
  353.                                                                                                             
  354.       /End-Free                                                                                             
  355.                                                                                                             
  356.      P prNxtword...                                                                                         
  357.      P                 E                                                                                    
  358.  
© 2004-2019 by midrange.com generated in 0.015s valid xhtml & css