ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Check if a record exists in file currently being read

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

  • Check if a record exists in file currently being read

    Hello,

    This is a pretty stupid question but I've not been shown otherwise - Quick example program, probably made a couple of mistakes please ignore them;

    Code:
    fFILE1     IF A E           K DISK    Prefix(pf_)             
    fFILE1L1   IF A E           K DISK    Prefix(l1_)             
    f                                     Rename(FILE1R : FILE1R1)
    
    d Level           S              2p 0 Inz(1)                  
    d NextLevel       S                   Like(Level)             
    
     /Free                                                        
    
       SetLl (Level) FILE1;                                       
       ReadE (Level) FILE1;                                       
    
       DoW Not %EoF(FILE1);                                       
    
         If pf_Object = 'Bad';                                    
    
           SetLl (Level : pf_Object) FILE1L1;                     
    
           If not %Equal(FILE1L1);                                
             Clear FILE1R1;          
             l1_Object = pf_Object;  
             l1_Level = pf_Level + 1;
             Write FILE1R1;          
           EndIf;                    
    
         ReadE (Level) FILE1;        
    
       EndDo;                        
    
       *Inlr = *On;                  
       Return;                       
    
     /End-Free

    Is there a way to check for a record in and/or check & then write to the file I am reading, without losing my place set by SetLl/ReadE? Or is the best way to do this just to have a logical with a matching key and check/write to that?

    I guess I could use select count(*) into :count for checking or define the same file twice with rename but I was wondering what other alternative options there are?

    Cheers,
    Ryan

  • #2
    I am not quite clear what you are trying to do. Is the intent to read a record and then under some circumstances update it? Or to write a new different record based on what you have read?

    The second SETLL - with no read in the "Bad" part is confusing me. And are these two views of the same file (physical and logical ?)

    Comment


    • #3
      There was a bug, sorry.

      This is just an example I tried to put together to try and help explain my question as opposed to me actually doing this.

      Correction with comments;

      Code:
       
       fFILE1     IF A E           K DISK    Prefix(pf_)              fFILE1L1   IF A E           K DISK    Prefix(l1_)              f                                     Rename(FILE1R : FILE1R1)  d Level           S              2p 0 Inz(1)                   d NextLevel       S                   Like(Level)                /Free                                                             // Read FILE1 by Level     SetLl (Level) FILE1;                                           ReadE (Level) FILE1;                                            DoW Not %EoF(FILE1);                                              // Looking for pf_Objects that are bad       If pf_Object = 'Bad';                                             // Found one; check if it already exists at NextLevel        NextLevel = Level + 1;         SetLl (NextLevel : pf_Object) FILE1L1;                              // If not, add it.        If not %Equal(FILE1L1);                                          Clear FILE1R1;                    l1_Object = pf_Object;            l1_Level = NextLevel;          Write FILE1R1;                  EndIf;                           // Get next object.      ReadE (Level) FILE1;             EndDo;                             *Inlr = *On;                      Return;                          /End-Free

      Comment


      • #4
        Ok, code tags didn't work.

        Attempt #3;

        Code:
        fFILE1     IF A E           K DISK    Prefix(pf_)             
        fFILE1L1   IF A E           K DISK    Prefix(l1_)             
        f                                     Rename(FILE1R : FILE1R1)
        
        d Level           S              2p 0 Inz(1)                  
        d NextLevel       S                   Like(Level)             
        
         /Free                                                        
        
           SetLl (Level) FILE1;                                       
           ReadE (Level) FILE1;                                       
        
           DoW Not %EoF(FILE1);                                       
        
             If pf_Object = 'Bad';                                    
        
               SetLl (Level : pf_Object) FILE1L1;                     
        
               If not %Equal(FILE1L1);                                
                 Clear FILE1R1;          
                 l1_Object = pf_Object;  
                 l1_Level = pf_Level + 1;
                 Write FILE1R1;          
               EndIf;                    
        
             ReadE (Level) FILE1;        
        
           EndDo;                        
        
           *Inlr = *On;                  
           Return;                       
        
         /End-Free

        Comment


        • #5
          You still haven't explained - in English - what you are trying to do. This code will certainly do something - but whether it is what you want or not I can't tell.

          Operations on FILE1L1 will have no impact on the cursor for FILE1 (unless you have specified a shared open data path). BUT - If they are indeed the same file then depending on the write action it may affect the record that your next read retrieves simply because there may be a new record in play.

          Comment


          • #6
            So you have a table that you're processing sequentially in keyed sequence, and you want to access the same table without losing the place in your sequential processing. Is that about right? You could either use SQL or add a second File Description spec over the same table.

            HTH

            Comment


            • #7
              Yes, that's it.

              So essentially there isn't another way of doing it other than defining it twice or using SQL.

              Cheers,
              Ryan

              Comment


              • #8
                Originally posted by RDKells View Post
                Yes, that's it.

                So essentially there isn't another way of doing it other than defining it twice or using SQL.

                Cheers,
                Ryan
                A third option would be to store the key and then reposition the table before resuming the loop.

                Comment


                • #9
                  Originally posted by RDKells View Post
                  So essentially there isn't another way of doing it other than defining it twice or using SQL.
                  There's always another way!

                  But, I don't really want to try to list every conceivable way to do something. Using a second copy of the file will work fine... why not do it that way?

                  Comment


                  • #10
                    I was just wondering what other options were available in case there was a better way of doing it.

                    Comment

                    Working...
                    X