ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

String Parsing in RPG IV

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

  • String Parsing in RPG IV

    Hi,

    I need some advice regarding this.
    I have an RPGLE program which calls a Java method the Java method returns a string to my RPGLE program. The string looks like this:

    John]999999999]John's address]@Tom]123456789]Tom's address]@#

    Actually the string is in this format:
    Name]SSN]Address@Name]SSN]Address@#
    ']' is the end of field marker
    '@' is the end of record marker
    '#' is the end of string marker.

    So, the string i have given as an example above has 2 records and 3 fields in each record.

    I need to extract each record in the above string and insert it in a physical file.
    The physical file is having the fields Name,SSN and Address.

    How can the above goal be achieved in the most efficient manner?


    Thanks.

  • #2
    Re: String Parsing in RPG IV

    I would probably use the %scan in a loop

    Something like this (untested)

    s = 0
    dou pos = 0
    eval pos = %scan(']':string:s+1)
    if pos > 0
    field1 = %subst(string:s+1: pos- (s+1))
    s = pos
    ... write field1 somewhere ...
    else leave
    enddo

    Comment


    • #3
      Re: String Parsing in RPG IV

      This should work. Im sure this loop can be simplified.

      You will need to add some validation to this routine to capture blank strings i.e ']]]@#'.

      Note: I had to remap my client access keyboard settings to allow the [ ].


      PHP Code:
      D String          S           1000    Varying                                       
      D orgS            S                   Like
      (String) 
      D EndPos          S             10I 0                                               
      D Pos             S             10I 0                                               
      D rb_Pos          S             10I 0                                               
      @_Pos           S             10I 0                                               
      D idx             S             10I 0                                               
      D Field1          S             20A                                                 
      D Field2          S             20A                                                 
      D Field3          S             20A                                                 
       
      *                                                                                  
       *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*              
       *                             
      Main                                  *          
       *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*          
       *                                                                              
       /
      free                                                                          
                                                                                      
          orgS 
      'John]999999999]Johns address]@Tom]123456789]Toms address]@#';  
          
      String orgS;                                                         
                                                                                      
          
      Dow %Trim(String) <> '#';                                                   
                                                                                      
            
      EndPos = %Scan'#' :String );                                            
            @
      _Pos = %Scan'@' :String );                                             
            
      rb_Pos = %Scan']' :String );                                            
                                                                                      
            For 
      idx 1 To 3;                                                         
              
      Select;                                                                 
                
      When idx 1;                                                         
                  
      Field1 = %SubStString ::rb_pos );                           
                
      When idx 2;                                                         
                  
      Field2 = %SubStString ::rb_pos );                           
                
      When idx 3;                                                 
                  
      Field3 = %SubStString ::rb_pos );                   
              
      EndSl;                                                          
                                                                              
              
      EndPos -= rb_Pos;                                               
              
      String = %SubStString :rb_Pos EndPos );                 
              @
      _Pos = %Scan'@' :String );                                   
                                                                              
              If ( @
      _Pos );                                               
                
      String =%SubStString :: %Len(%Trim(String)) - 1);         
              EndIf;                                                          
                                                                              
              
      rb_Pos = %Scan']' :%Subst(String ::@_Pos ));                
                                                                              
                                                                              
            EndFor;                                                           
          
      EndDo
      Last edited by kpmac; May 11, 2006, 12:14 PM.
      Predictions are usually difficult, especially about the future. ~Yogi Berra

      Vertical Software Systems
      VSS.biz

      Comment


      • #4
        Re: String Parsing in RPG IV

        This is an old thread, but I'm having a little trouble with the same sort of issue. In CGIDEV2, I'm receiving a string like 01/15/2009 and I need to separate each day, month, and year into separate fields.

        I understand the following code:

        Code:
        s = 0
        dou pos = 0
        eval pos = %scan(']':string:s+1)
        if pos > 0
        This line is where I get a little lost. I understand that I would be setting field1 equal to a portion of string. However, I don't really understand how to specify what portion.

        Code:
        field1 = %subst(string:s+1: pos- (s+1))

        Comment


        • #5
          Re: String Parsing in RPG IV

          something like this:
          Code:
          dmydate           s               d                           
          dmyday            s              2s 0                         
          dmymonth          s              2s 0                         
          dmyyear           s              4s 0                         
          dparmdate         s             10a   Inz('01/01/2009')       
           /free                                                        
             mydate = %date(parmdate:*USA/);                            
             mymonth = %subdt(mydate:*months);                          
             myday = %subdt(mydate:*days);                              
             myyear = %subdt(mydate:*years);                            
             *inlr = *On;                                               
             return;
          date functions rock!!!
          I'm not anti-social, I just don't like people -Tommy Holden

          Comment


          • #6
            Re: String Parsing in RPG IV

            Originally posted by violinsoundcool View Post

            This line is where I get a little lost. I understand that I would be setting field1 equal to a portion of string. However, I don't really understand how to specify what portion.

            Code:
            field1 = %subst(string:s+1: pos- (s+1))
            At the time this code runs, pos contains the position of the end marker for the current string and s contains the position of the end of the previous string (or 0 if there were none).

            The way that %Subst works is that parameter 2 identifies the start position for the string to extract. Since this is of course one character further on than the end of the last string adding 1 to s gives us the start point (i.e. s + 1). The third parameter is the length of string to be extracted which is calculated from the difference between the end position of the previous string and the end position of the current one + 1 (i.e. pos - (s + 1) ).

            Personally I'd have added the 1 to s (and maybe called it startPos!) when it was created and avoided the repeated + 1 bit but ...

            To more directly answer your question - the method I use would depend on whether validation of the date is needed, whether I can guarantee that it has two digits in each position etc. etc.


            JonP

            Comment


            • #7
              Re: String Parsing in RPG IV

              Tom has the method I like best for dates. I would just add a MONITOR before the first calc to catch a bad date error rather than get a hard halt.

              Comment


              • #8
                Re: String Parsing in RPG IV

                Originally posted by arrow483 View Post
                Tom has the method I like best for dates. I would just add a MONITOR before the first calc to catch a bad date error rather than get a hard halt.
                The big problem is that if (say) m/d/yyyy is acceptable as input, then any date based model will fall into the error routine very frequently.

                Under those circumstances a routine based on scanning for the "/" characters will work much better. Still easy to create a real date from the resulting strings if needed.


                Jon P.

                Comment


                • #9
                  Re: String Parsing in RPG IV

                  The big problem is that if (say) m/d/yyyy is acceptable as input, then any date based model will fall into the error routine very frequently.
                  Sure? As long as a 4 digit year format (*USA, *EUR, *JIS, *ISO) is used, single digit month and day are handled correctly. But it's not supported for a 2 digit year format (*YMD, *DMY, *MDY).

                  The following example works correctly:
                  PHP Code:
                   &#37;
                  D DSDate          DS                                         
                  D                               10A   inz
                  ('12/31/2008')      
                  D                               10A   inz('01/31/2009')      
                  D                               10A   inz('1/31/2009')       
                  D                               10A   inz('12/01/2008')      
                  D                               10A   inz('12/1/2008')       
                  D                               10A   inz('03/05/2009')      
                  D                               10A   inz('3/5/2009')      
                  D   ArrDate                     10A   Overlay(DSDateDim(7

                  D PGMSDS         SDS                 
                  D   SDSMsgTxt            91    140   
                   
                  /Free                                                 
                       
                  For Index 1 to %Elem(ArrDate);                              
                           
                  Monitor;                                                  
                              
                  Dsply (%Date(ArrDate(Index): *USA));                   
                              
                  Dsply (%SubDt(%Date(ArrDate(Index): *USA): *Y));       
                              
                  Dsply (%SubDt(%Date(ArrDate(Index): *USA): *M));       
                              
                  Dsply (%SubDt(%Date(ArrDate(Index): *USA): *D));       
                           
                  On-Error;                                                 
                              
                  Dsply SDSMsgTxt;                                       
                           
                  EndMon;                                                   
                       EndFor;
                       *
                  INLR = *On;                                                       
                   /
                  End-Free 
                  Birgitta

                  Comment


                  • #10
                    Re: String Parsing in RPG IV

                    Originally posted by B.Hauser View Post
                    But it's not supported for a 2 digit year format (*YMD, *DMY, *MDY).
                    And I would certainly hope, after the programming fiasco we had leading up to the current century, that noone is sending 2 digit years anymore.

                    JonBoy,
                    You should always use the date bif's. Not only are they self-documenting, but if a field errors out on a %date() bif, then simply scanning the field and reconstructing the date will never work and be 100% accurate. If someone sending me data can not get the date into a standardized format, then there is no code that could be written that could accurately reconstruct it.
                    Michael Catalani
                    IS Director, eCommerce & Web Development
                    Acceptance Insurance Corporation
                    www.AcceptanceInsurance.com
                    www.ProvatoSys.com

                    Comment


                    • #11
                      Re: String Parsing in RPG IV

                      Originally posted by B.Hauser View Post
                      Sure? As long as a 4 digit year format (*USA, *EUR, *JIS, *ISO) is used, single digit month and day are handled correctly. But it's not supported for a 2 digit year format (*YMD, *DMY, *MDY).
                      Thanks Birgitta - This is both embarrassing and funny - I had originally thought it would work because it works for an L datatype input on the 5250 - but I decided to test it before writing. Simply put it turns out I screwed up the test. I'm too embarrassed to say why.

                      I'll put it down to my great age and yesterday being my birthday. Doesn't make me feel any better but ...

                      Not working for the 2 digit variants makes no sense - I'll see if Barbara can explain that.

                      Sorry for the misinformation - and thanks for catching me.

                      Jon P.

                      Comment


                      • #12
                        Re: String Parsing in RPG IV

                        Originally posted by MichaelCatalani View Post
                        And I would certainly hope, after the programming fiasco we had leading up to the current century, that noone is sending 2 digit years anymore.
                        I'm glad you added a smiley because in my experience there are still a HUGE number of people using 2 digit years. Goodness knows why - only reason I can think is it saves a step when they display/print it becuase end users still seem to want to see a 2 digit version.

                        JonBoy,
                        You should always use the date bif's. ...
                        Or use TEST if there is no date math needed - after all why add the overhead of converting to a "real" date if you're not doing anything with the result.

                        I actually helped design the date math BIFs and other similar features of RPG IV so I have no hesitation in recommending them

                        But equally have no excuse for having screwed up the simple test that led to me making such a dumb-@#%@#%@#% statement in the first place!

                        JonP

                        Comment


                        • #13
                          Re: String Parsing in RPG IV

                          Originally posted by JonBoy View Post
                          ... I'll put it down to my great age and yesterday being my birthday. Doesn't make me feel any better but ....
                          Heck! I looked at your age and saw 120!! then realized it was # of posts Happy birthday, hope you had a great day.
                          Originally posted by JonBoy View Post
                          ... I actually helped design the date math BIFs and other similar features of RPG IV so I have no hesitation in recommending them ....
                          Would it be possible that you could help them tidy up/upgrade/fix the Date field functionality in DSPF's?
                          Regards

                          Kit
                          http://www.ecofitonline.com
                          DeskfIT - ChangefIT - XrefIT
                          ___________________________________
                          There are only 3 kinds of people -
                          Those that can count and those that can't.

                          Comment


                          • #14
                            Re: String Parsing in RPG IV

                            Originally posted by kitvb1 View Post
                            ... Would it be possible that you could help them tidy up/upgrade/fix the Date field functionality in DSPF's?
                            Different part of IBM I'm afraid - Toronto do the compilers, Rochester the DDS. And I can't see anyone at IBM wanting to spend money on enhancing DDS at this point in time.

                            Jon P

                            Comment


                            • #15
                              Re: String Parsing in RPG IV

                              happy birthday Jon...sorry i missed it also.......

                              Your only as old as you feel

                              jamie
                              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