ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Read infinite Loop

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

  • Read infinite Loop

    Hello,

    I have 1 physical file with 4 entries. Im reading this file with DOW not %eof and a read but i stuck at the first record and it goes to a infinite loop.
    I dont know why and how can i get to the next entry ? i tried it with iter but doesnt work.

    Code:
    /free                  
     Read TEST;           
       dow not %eof;       
       do something          
       ExSr $testsr;        
       enddo;              
     *inlr=*on;            
    /end-free              
    Endsr;

  • #2
    Your only read is outside your loop, so it never reads more than one row.

    Comment


    • #3
      When you use this technique (often referred to as a priming read), you also need to be sure to include a read _within_ the loop. in this case it should precede the enddo.
      Last edited by JonBoy; July 9, 2020, 09:44 AM.

      Comment


      • #4
        Ok i just solved the problem but then i look at my output the programm reads the last record twice. How can i solve this ?

        Comment


        • #5
          What does your code look like now so we can help?

          Comment


          • #6
            Hard to say when we don't know what you changed your code to. Can you share the updated code?

            Personally I prefer this to a priming read:
            Code:
            DoU %EOF(TEST);
              Read TEST;
              If %EOF(TEST);
                Leave;
              EndIf;
              // do something
            
            EndDo;
            That way there is only one line where the record is read. It's not as big an issue with native IO, but in SQL the equivalent to a READ is a FETCH statement, which can be a lot larger than "READ x"

            P.S. you should always include the filename in %FOUND and %EOF, as in my example. Otherwise it merely refers to the last IO operation performed. So if somewhere within //do something you were doing another READ on another table, it could affect TEST's loop

            Comment


            • JonBoy
              JonBoy commented
              Editing a comment
              Interesting. I dislike this method because it makes it harder to differentiate between first read and subsequent. EOF on first read is probably/possibly an error (i.e. empty input file). But on all other reads it is an expected condition. with your approach it is much harder to differentiate the two. I should not that I invariably add a comment

              // Loop priming read

              Or something similar to warn those who use your technique.
          Working...
          X