ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Do Loop Problem

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

  • Do Loop Problem

    The first record of the SAC has PO# 12345 and Item: ABCDE.

    The ex_850dtly file has 2 records with that PO and Item combination. However, it seems to ignore the "dou not %found(ex_850dtly)" command and goes through the loop the 3rd time. when it hits the UPDATE, it bombs out.

    Code:
    /free                                                
       read ex_850sac;                                   
       dou %eof(ex_850sac);                              
         setll (edsacpo:edsacitm) ex_850dtly;            
         reade (edsacpo:edsacitm) ex_850dtly;            
         dou not %found(ex_850dtly);                     
           item = %subst(edpo107:1:6);                   
           color = %subst(edpo113:1:2);                  
           if %subst(edsacupc:2:5) = %subst(edpo107:1:5);
             size = '000';                               
           else;                                         
             size = %subst(edpo113:3:3);                 
           endif;                                        
           edpo118 = 'IS';                               
           edpo119 = item + '-' + color + '-' + size;    
           edpo120 = 'MU';                               
           edpo121 = edsacupc;                           
           update edformat;                              
           reade (edsacpo:edsacitm) ex_850dtly;          
         enddo;                                          
         read ex_850sac;                                 
       enddo;                                                 
       *inlr = *on;                                           
       return;                                                
    /end-free

  • #2
    Re: Do Loop Problem

    should be dou not %eof(ex_850dtly);
    I'm not anti-social, I just don't like people -Tommy Holden

    Comment


    • #3
      Re: Do Loop Problem

      Tom,
      Wouldn't that be....

      DOW not DOU ??
      Your future President
      Bryce

      ---------------------------------------------
      http://www.bravobryce.com

      Comment


      • #4
        Re: Do Loop Problem

        Originally posted by bryce4president View Post
        Tom,
        Wouldn't that be....

        DOW not DOU ??
        well i wasn't going to get into code bashing but use the code presented. structurally speaking i see a lot of things i personally would have done differently.
        I'm not anti-social, I just don't like people -Tommy Holden

        Comment


        • #5
          Re: Do Loop Problem

          I'm definitely agree, but the statement DOU NOT %EOF() won't even return any records. Its not logically possible
          Your future President
          Bryce

          ---------------------------------------------
          http://www.bravobryce.com

          Comment


          • #6
            Re: Do Loop Problem

            Originally posted by bryce4president View Post
            I'm definitely agree, but the statement DOU NOT %EOF() won't even return any records. Its not logically possible
            actually it will. there's a priming read before the do loop. (which i prefer if using DOU for the read to be the first opcode after the DOU.
            I'm not anti-social, I just don't like people -Tommy Holden

            Comment


            • #7
              Re: Do Loop Problem

              here's a better way IMO
              Code:
                    /free
                        dou %eof(ex_850sac);
                          read ex_850sac;
                          if %eof(ex_850sac);
                            leave;
                          endif;
              
                          setll (edsacpo:edsacitm) ex_850dtly;
                          dou %eof(ex_850dtly);
                            reade (edsacpo:edsacitm) ex_850dtly;
                            if %eof(ex_850dtly);
                              leave;
                            endif;
              
                            item = %subst(edpo107:1:6);
                            color = %subst(edpo113:1:2);
                            if %subst(edsacupc:2:5) = %subst(edpo107:1:5);
                              size = '000';
                            else;
                              size = %subst(edpo113:3:3);
                            endif;
                            edpo118 = 'IS';
                            edpo119 = item + '-' + color + '-' + size;
                            edpo120 = 'MU';
                            edpo121 = edsacupc;
                            update edformat;
                          enddo;
                        enddo;
              
                        *inlr = *on;
                        return;
                    /end-free
              Last edited by tomholden; August 7, 2008, 03:01 PM.
              I'm not anti-social, I just don't like people -Tommy Holden

              Comment


              • #8
                Re: Do Loop Problem

                DOU will loop through once even if the test fails. Thats probably what was causing the bomb
                Goodbye

                Comment


                • #9
                  Re: Do Loop Problem

                  Right. DOU will ALWAYS run at least once because it tests the condition on the ENDDO statement. DOW tests the condition on the DOW statement. You should almost always use a DOW statement. (I think I have used a DOU maybe once or twice in 20 years of programming.)

                  Comment


                  • #10
                    Re: Do Loop Problem

                    Originally posted by soup_dog View Post
                    Right. DOU will ALWAYS run at least once because it tests the condition on the ENDDO statement. DOW tests the condition on the DOW statement. You should almost always use a DOW statement. (I think I have used a DOU maybe once or twice in 20 years of programming.)
                    this can be a my dad is better than your dad war. i've only used DOW a couple of times in my career. using either one can accomplish the same goal...it's how you handle exceptions and code appropriately for conditions like the issue mentioned above. i like DOU because it will always enter the loop and check for exit conditions based on the criteria required.
                    I'm not anti-social, I just don't like people -Tommy Holden

                    Comment


                    • #11
                      Re: Do Loop Problem

                      It is a "to each his own" kind of thing. I prefer DOW loops since they don't require a check to see if the loop should be exited.

                      DOU
                      If Condition
                      Leave
                      Endif
                      ENDDO

                      Versus

                      DOW
                      ...
                      ENDDO

                      I can read and understand either, but like the original posters problem, it is not intuitive that the DOU will process once regardless of the test.

                      Again, to each his own, but it is human nature to think that yours is the best way
                      Goodbye

                      Comment


                      • #12
                        Re: Do Loop Problem

                        Originally posted by KenM View Post
                        It is a "to each his own" kind of thing. I prefer DOW loops since they don't require a check to see if the loop should be exited.

                        DOU
                        If Condition
                        Leave
                        Endif
                        ENDDO

                        Versus

                        DOW
                        ...
                        ENDDO

                        I can read and understand either, but like the original posters problem, it is not intuitive that the DOU will process once regardless of the test.

                        Again, to each his own, but it is human nature to think that yours is the best way
                        i kinda implied that...they both perform the same functionality but it's still up to the programmer to ensure that the loop handling is correct...
                        I'm not anti-social, I just don't like people -Tommy Holden

                        Comment


                        • #13
                          Re: Do Loop Problem

                          Nope. My way is the best way. Nyaah Nyaah!!!!!

                          Comment

                          Working...
                          X