ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Backward %Scan

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Backward %Scan

    Is there something that finds the first occurance scanning backward? It looks like%checkr does not work because it finds the first occurance something does not happen.

    Example: string is aaaxaaaxaa
    pos = %scanr('x' : source); would return 8
    pos = %scanr('x' : source : 6); would return 4

    The only way I can see to do this is a do-loop or a for-loop where you %subst one character at time moving backward through the string.
    http://www.linkedin.com/in/chippermiller

  • #2
    Re: Backward %Scan

    You could reverse the string but you would still need a loop to achieve this.
    Ben

    Comment


    • #3
      Re: Backward %Scan

      If the string "aaaxaaaxaa" is reversed the result is "aaxaaaxaaa"
      pos = %scan('x' : source); would return 3 not 8

      The best way is to write a function for this.
      Patrick

      Comment


      • #4
        Re: Backward %Scan

        Originally posted by K2r400 View Post
        If the string "aaaxaaaxaa" is reversed the result is "aaxaaaxaaa"
        pos = %scan('x' : source); would return 3 not 8

        The best way is to write a function for this.
        I was fairly sure the only way to do this would be a loop. Not difficult to code -- just makes the machine do a lot of grunt work.

        A function would be a good idea too -- would be generic for any situation. If I can throw that together, will share here on Code/400 as a code sample.
        http://www.linkedin.com/in/chippermiller

        Comment


        • #5
          Re: Backward %Scan

          hi Chipper:

          using the scan function in a loop you could find the LAST X position and subtract the field length from that position

          I don't know that this solution is any better ..... just an alterantive

          GLS
          The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

          Comment


          • #6
            Re: Backward %Scan

            I wrote a Cobol function using the Cobol Built-in FUNCTION REVERSE() many moons ago. This function reverses the order of the characters of the character argument. The length of the argument can be up to 1024 that you can broaden or shrink in the source programs below if necessary. You can use the function in conjunction with the %SCAN BIF as shown below.

            Cobol Function REVERSE

            To compile

            1/ CRTCBLMOD MODULE(MyLib/REVERSE)
            SRCFILE(MyLib/QCBLLESRC)
            SRCMBR(REVERSE)

            2/ CRTSRVPGM MODULE(*SRVPGM)
            SRVPGM(MyLib/REVERSE)
            EXPORT(*ALL)

            Code:
                    IDENTIFICATION DIVISION. 
                             ************************* 
                             * [COLOR="darkred"]CRTCBLMOD + CRTSRVPGM[/COLOR] * 
                             ************************* 
                   PROGRAM-ID.        REVERSE.
                   ENVIRONMENT DIVISION.
                   CONFIGURATION SECTION.
                   SOURCE-COMPUTER.   ibm-iseries.
                   OBJECT-COMPUTER.   ibm-iseries.
                   DATA DIVISION.
            
                   WORKING-STORAGE SECTION.
                   01  OutputString.
                       05  RevLength Pic S9(3) Binary value 0.
                       05  RevString.
                        10  RevData  Occurs 1 to 1024 Depending On RevLength Pic X.
            
                   LINKAGE SECTION.
                   01  InputString.
                       05 InLength Pic S9(3) Binary.
                       05 InString Pic X(1024).
            
                   PROCEDURE DIVISION Using InputString Returning OutputString.
                   Begin.
                       Move InLength to RevLength.
                       Move Function Reverse(InString(1:InLength))
                                                     to RevString(1:RevLength).
                       GoBack.

            How to use function REVERSE in RPG IV


            Add service program REVERSE to BNDDIR 'REVERSE'.

            Code:
            H dftactgrp( *no ) 
            H Bnddir('REVERSE')
            D reverse         pr          1024a   varying 
            D                                     extproc( 'REVERSE' ) 
            D  InString                   1024a   const varying       
            
            D  pos            s             10i 0 inz 
            D  InString       s           1024a    varying  inz('aaaxaaaxaa') 
             
                     
               /free
                    pos = %scan('x' : [COLOR="darkred"]reverse(InString)[/COLOR]);
            and you're done.
            Last edited by Mercury; December 17, 2010, 10:09 AM.
            Philippe

            Comment


            • #7
              Re: Backward %Scan

              Just did go licpgm and don't have Cobol -- maybe there's something in Java or C that I could use.
              http://www.linkedin.com/in/chippermiller

              Comment


              • #8
                Re: Backward %Scan

                No example in C, only in C#

                PHP Code:
                public string Reverse(string str)
                {
                    
                int len str.Length;
                    
                char[] arr = new char[len];

                    for (
                int i 0leni++)
                    {
                        
                arr[i] = str[len i];
                    }

                    return new 
                string(arr);

                Philippe

                Comment


                • #9
                  Re: Backward %Scan

                  read my previous response, reverse it's not the solution.

                  Below the function :
                  Code:
                  p Scanr           b                                            
                  d Scanr           pi            10u 0                          
                  d  pArgSearch                32565    Varying Const            
                  d  pData                     32565    Varying Const            
                  d  pStartPos                    10u 0 options(*nopass) Const   
                                                                                 
                  d StartPos        s                   like(pStartPos)          
                  d Data            s                   like(pData)              
                  d x               s              3u 0                          
                  d RetValue        s             10u 0                          
                                                                                 
                   /free                                                         
                      if %parms = 3;                                             
                         StartPos = pStartPos;                                   
                      Endif;                                                     
                      if StartPos = 0 or                                         
                         StartPos > %len(pData);                    
                         StartPos = %len(pData);                    
                      Endif;                                        
                      Data = %subst(pData : 1 : StartPos);          
                      Clear x;                                      
                      dou x = 0;                                    
                         x = %scan(pArgSearch : Data : x + 1);      
                         if x <> 0;                                 
                            RetValue = x;                           
                         endif;                                     
                      enddo;                                        
                      Return RetValue;                              
                   /end-free                                        
                  p Scanr           e
                  Patrick

                  Comment


                  • #10
                    Re: Backward %Scan

                    Originally posted by Chipper View Post
                    Just did go licpgm and don't have Cobol -- maybe there's something in Java or C that I could use.
                    You could use the lastIndexOf method of the String class. Remember that Java indexes start from 0 not 1. The downside to this is the time it may take to start the JVM. There may be a C equivalent but I don't know much C. C# obviously has the lastIndexOf method available meaning the reverse function would not have been required.

                    To do it in RPG I would take the one off hit of reversing the string and then scan as normal. Patrick points out the indexes would be wrong but you could easily correct these. I think this method would require the least looping or repeated effort.
                    Ben

                    Comment


                    • #11
                      Re: Backward %Scan

                      Did anyone else's eyes start burning when they read the COBOL source? Or, was it just me??

                      Comment


                      • #12
                        Re: Backward %Scan

                        I think it was just you....That is a tiny tiny COBOL program.
                        (I've seen bigger CL programs)
                        All my answers were extracted from the "Big Dummy's Guide to the As400"
                        and I take no responsibility for any of them.

                        www.code400.com

                        Comment

                        Working...
                        X