ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Like over linkage section data

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

  • Like over linkage section data

    COBOL requires that a variable that is defined with LIKE be defined after the variable upon which it is patterned. This compiles:

    Code:
    01  AAA          pic x(20).
    01  BBB          like AAA.
    This does not:

    Code:
    01  BBB          like AAA.
    01  AAA          pic x(20).
    This means that there is no way for a working-storage variable to be defined like a linkage section variable. Or am I mistaken?

  • #2
    Re: Like over linkage section data

    Interesting thought...but it didn't work. I would think that the compiler would have to make another pass over the code to adjust its storage layout in order to be feasible...

    working-storage section.
    01 Eng Like Branch.

    linkage section.

    01 Branch pic x(12).

    procedure division
    using Branch.

    * 161 MSGID: LNC1236 SEVERITY: 30 SEQNBR: 004500
    Message . . . . : No PICTURE for elementary item 'ENG'.
    * 161 MSGID: LNC2515 SEVERITY: 30 SEQNBR: 004500
    Message . . . . : LIKE object 'BRANCH' not defined. Clause discarded.

    Comment


    • #3
      Re: Like over linkage section data

      Sorry, Ted. I got nothin'.

      Well, other than the obvious, I suppose... Declare the LINKAGE item LIKE the WORKING STORAGE item instead of the other way around. Or maybe better, have basic declares at the top of WORKING STORAGE, and declare the actual variables in both WORKING STORAGE and LINKAGE LIKE the basic ones. I.e., create the "basic" ones for use as 'templates'.

      Hard to think that something like that hasn't already crossed your mind, so I suspect there's something about it that gives a bad taste.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4
        Re: Like over linkage section data

        I want to put a copybook in the linkage section. I want working-storage variables like some of those in the copybook.

        If I could copy the copybook into working-storage instead of the linkage section, that would be OK too, but I can't find a way to make that work.

        So far, it appears that I will have to hard-code the lengths of the working-storage variables.

        Comment


        • #5
          Re: Like over linkage section data

          This is something a little obtuse ;-) I used this once...but can't remember why. Use a Physical File description to get variables into Working-Storage and the Linkage Section.

          If its a common group of variables, all you need to do is recreate your physical and recompile any dependent objects...

          Code:
            
            154     003800 working-storage section.   
            169     005500 01  WS-AREA.                                                       
                      005600               copy ddr-WSTORAGE   of AAAF                          
                      005700                     prefix by 'WS-'.                               
                     +000001*    I-O FORMAT:WSTORAGE   FROM FILE AAAF       OF LIBRARY ISTW     
                     +000002*                                                                   
            170    +000003       05  WSTORAGE.                                                
            171    +000004           06 WS-ACCOUNTKEY         PIC S9(5)         COMP-3.       
            172    +000005           06 WS-NAME               PIC X(20).                      
            173    +000006           06 WS-ADDRESS            PIC X(20).                      
            174    +000007           06 WS-CITY               PIC X(20).                      
            175    +000008           06 WS-STATE              PIC X(2).                       
            176    +000009           06 WS-ZIP                PIC S9(5)         COMP-3.       
            177    +000010           06 WS-BALANCE            PIC S9(11)V9(2).
          Code:
            
            519     012000 linkage section.   
            521     012400 01  Link-AREA.                                                 
                      012500               copy ddr-WSTORAGE   of AAAF                      
                      012600                     prefix by 'Link-'.                         
                     +000001*    I-O FORMAT:WSTORAGE   FROM FILE AAAF       OF LIBRARY ISTW 
                     +000002*                                                               
            522    +000003       05  WSTORAGE.                                            
            523    +000004           06 Link-ACCOUNTKEY       PIC S9(5)         COMP-3.   
            524    +000005           06 Link-NAME             PIC X(20).                  
            525    +000006           06 Link-ADDRESS          PIC X(20).                  
            526    +000007           06 Link-CITY             PIC X(20).                  
            527    +000008           06 Link-STATE            PIC X(2).                   
            528    +000009           06 Link-ZIP              PIC S9(5)         COMP-3.   
            529    +000010           06 Link-BALANCE          PIC S9(11)V9(2).
          based on this DDS:

          Code:
          R WSTORAGE                                   
             #CCNTKEY        5P 0       ALIAS(ACCOUNTKEY)
             #NAME           20A          ALIAS(NAME)      
             #ADDR           20A          ALIAS(ADDRESS)   
             #CITY            20A          ALIAS(CITY)      
             #STATE           2A          ALIAS(STATE)     
             #ZIP               5P 0        ALIAS(ZIP)       
             #BALANCE      13S 2        ALIAS(BALANCE)
          Not rocket science but could be useful in some situations.

          Comment


          • #6
            Re: Like over linkage section data

            Originally posted by TedHolt View Post
            ...but I can't find a way to make that work.
            What problem are you running into?

            The COPY statement is fairly easy and flexible in COBOL, but I suppose it depends on how the structures are set up. I have a set of 01-level and other items for IFS functions that I bring in like this:
            Code:
             FMT CB ......-A+++B++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Pgm-id++
            0084.00
            0085.00            copy IFSCBL.
            0086.00
            That's as simple as an example can get, I guess. This next one goes one step farther with two COPY members that have the same data item names, but numerous data items have different numeric data types. I used these back before RPG got the EVAL-CORR and COBOL was good at doing quick reformats of complex data structures with MOVE CORR:
            Code:
             FMT **  ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
            0054.00        01  XFM-PARMS.
            0055.00            copy '@AJ5SVD'.
            0056.00
            0057.00        Linkage Section.
            0058.00
            0059.00        01  LK-PARMS.
            0060.00            copy '@AJ5SV'.
            Mostly it all just depends on what levels things are done at. But maybe you have a specific problem. Can you show what isn't working? In my second example, I could've used various LIKEs instead of a second COPY except many of the items weren't "like" the WORKING STORAGE versions.
            Tom

            There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

            Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

            Comment


            • #7
              Re: Like over linkage section data

              Originally posted by tomliotta View Post
              What problem are you running into?
              Obviously I can copy the copybook into working-storage. I can't find a way to assign the address of the linkage section variable to the working-storage variable, as I could with a based data structure in RPG, so that modifying the data in working-storage would really be modifying the data in the caller.

              I could copy the data from the linkage section to working-storage at program entry and copy it back at program exit. I don't want to do that because the structure defined in the copybook is huge and performance is critical.

              I have hard-coded the lengths of my working variables. Chances that they'll change are slim to none. I just have a habit of using like to define variables when possible to reduce the possibility of introducing bugs when a program is modified.

              Comment


              • #8
                Re: Like over linkage section data

                You could use pointers to access your work areas:

                Code:
                       working-storage section.                             
                       01  New-Pointer usage is pointer.                    
                       01  Old-Pointer usage is pointer.                    
                                                                            
                       01  WS-AREA.                                         
                           05  WS-CCYY         pic  x(04)     value spaces. 
                           05  WS-MM           pic  x(02)     value spaces. 
                           05  WS-DD           pic  x(02)     value spaces. 
                                                                            
                       linkage section.                                     
                                                                            
                       01  Link-AREA.                                       
                           05  Link-CCYY         pic  x(04).                
                           05  Link-MM           pic  x(02).                
                           05  Link-DD           pic  x(02).                
                                                                            
                       procedure division                                   
                                    using Link-AREA                      
                           .                                             
                       100-Begin.                                        
                                                                         
                           initialize Link-AREA                          
                                      WS-AREA                            
                                                                         
                           set New-Pointer    to address of WS-AREA      
                           set Old-Pointer    to address of Link-AREA    
                                                                         
                           move '20150619'    to WS-AREA                 
                                                                         
                           display '  WS-AREA='    WS-AREA               
                           display 'Link-AREA='    Link-AREA             
                                                                         
                           set address of Link-AREA to New-Pointer       
                           move '19560422'    to WS-AREA        
                                                                
                           display '  WS-AREA='    WS-AREA      
                           display 'Link-AREA='    Link-AREA

                Comment


                • #9
                  Re: Like over linkage section data

                  Originally posted by TedHolt View Post
                  I can't find a way to assign the address of the linkage section variable to the working-storage variable, as I could with a based data structure in RPG, so that modifying the data in working-storage would really be modifying the data in the caller.
                  True. But you shouldn't have to since you can simply modify the LINKAGE item. Why would it be necessary to reference the LINKAGE item through a definition in WORKING-STORAGE?

                  Okay, I only asked that question out of curiosity. The answer would clarify a lot about this problem.

                  However, I think I finally grasp what the problem is from your perspective. What you apparently actually need to do is NOT define the WORKING-STORAGE data item at all. Instead, declare that item as a LINKAGE item.

                  A LINKAGE item isn't required to be referenced in a USING clause. That is, it's not required to be used for "linkage". It can be any definition that can be referenced as long as it has an address assigned for it. In your case, think of a new LINKAGE item as if it was a RPG BASED DS. Define it and use it after setting its ADDRESS OF to be the ADDRESS OF your previous LINKAGE item. The practical problem seems to be only that you were thinking you had to define things only in WORKING-STORAGE and use LINKAGE items for "linkage" only..

                  And note that if you define it after the first LINKAGE item, the LIKE clause becomes valid.

                  Make sense?
                  Tom

                  There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                  Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                  Comment


                  • #10
                    Re: Like over linkage section data

                    Thanks, Tom & Terry for the many thoughts and ideas. You've convinced me that I cannot do what I want to do.

                    Here's one more illustration. I offer it as one last attempt to make sure that I'm not overlooking anything.

                    Here's a copybook.

                    Code:
                    01  Important-data.                                         
                        02  Customer-number       pic x(8).                     
                        02  Customer-name         pic x(20).                    
                        02  Customer-shoe-size    pic s99v9      packed-decimal.
                        02  Customer-net-worth    pic s9(11)v99  packed-decimal.
                    I want some work variables in my program that must be defined like the corresponding variables in the copybook.

                    Code:
                     Identification division.
                     Program-ID.    qad04281l.
                    
                     Environment division.
                    
                     Data division.
                    
                     Working-storage section.
                    
                    *** what I have to do
                     01  Hold-Shoe-size      pic s99v9        packed-decimal.
                     01  New-net-worth       pic s9(11)v99    packed-decimal.
                    
                    *** what I would prefer to do
                    *01  Hold-Shoe-size      like Customer-shoe-size.
                    *01  New-net-worth       like Customer-net-worth.
                    
                     Linkage section.
                    
                     copy qad0428cpy in qad.
                    
                     Procedure division using Important-data.
                     000-Main-logic.
                         move Customer-shoe-size to Hold-shoe-size
                         if Customer-shoe-size > 8.5
                             continue
                         else
                             continue
                         end-if
                         move New-net-worth to Customer-net-worth
                         goback.
                    It seems like such a simple thing, doesn't it?
                    Last edited by TedHolt; June 20, 2015, 08:22 AM.

                    Comment


                    • #11
                      Re: Like over linkage section data

                      Originally posted by tomliotta View Post
                      True. But you shouldn't have to since you can simply modify the LINKAGE item. Why would it be necessary to reference the LINKAGE item through a definition in WORKING-STORAGE?
                      It's not necessary, Tom. That's not what I wanted to do. My musings inadvertently misled you.

                      Comment


                      • #12
                        Re: Like over linkage section data

                        Originally posted by TedHolt View Post
                        *** what I would prefer to do
                        Ah, that gets us closer. And maybe we can actually make it work, though it won't be quite as easy as RPG would be. I had a few old test members that I've pulled pieces from to create an example. As is often true, don't try to make logical sense of what this code does as a program. The pieces I've chosen only result in a *PGM that compiles and runs. The executable statements don't make sense in the context of this example, but they did make sense in the original test procs. Running under debug shows that it all works as intended.

                        What this does is show that the LINKAGE item NUM3 can be defined LIKE the LINKAGE item NUM2. And then the value of NUM2 can be assigned to NUM3, perhaps for holding it for later comparisons.

                        The messy part is that NUM3 has no memory allocated when the program starts. The memory has to be sized and malloc'd. Once that's done, the ADDRESS OF NUM3 can be set. Not hugely messy, but it does take some coding.
                        Code:
                               PROCESS APOST
                                       NOMONOPRC
                               Identification Division.
                               Program-ID.       '@TST0I' .
                        
                               Environment Division.
                        
                               Configuration Section.
                               Source-computer.    IBM-AS400.
                               Object-computer.    IBM-AS400.
                               Special-names.      System-Shutdown is  shutdown-switch
                                                       ON status   is  going-down
                                                   Linkage type is prc
                                                      for 'malloc'.
                               INPUT-OUTPUT SECTION.
                        
                               FILE-CONTROL.
                               DATA DIVISION.
                               FILE SECTION.
                        
                               WORKING-STORAGE SECTION.
                        
                               01  Num1                        pic s9(4)
                                                                   value  9999
                                                                   usage comp-4.
                        
                               LINKAGE SECTION.
                        
                               01  Num2                        pic s9(4)
                                                                   usage comp-4.
                               01  Num3    like    Num2.
                        
                               Procedure Division.
                        
                               00-Main-driver.
                        
                                   call 'malloc'
                                       using   by value LENGTH OF Num3
                                       giving  ADDRESS OF Num3
                        
                                   compute Num1 =  Num1 + 9999.
                                   compute Num1 =  65000.
                                   compute Num1 =  66000.
                                   set ADDRESS OF Num2 to ADDRESS OF Num1
                                   compute Num2 =  66000.
                                   compute Num3 =  Num2
                        
                                   goback.
                        The address reference for NUM2 going back to NUM1 isn't meaningful except in how it somewhat mimics parameter addressing for LINKAGE. And the NUM1 stuff is just stuff that gives us something in memory to look at.

                        One missing item is a COPY that brings NUM2 into the source. I can set one up, but that can wait until you think this is even worth going forward with. It works. Maybe it's just not worth it.
                        Tom

                        There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                        Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                        Comment


                        • #13
                          Re: Like over linkage section data

                          I don't see how you can ever use LIKE because of the requirement to have the object already predefined. Here's something a little obtuse - its a slow workday ;-)

                          Code:
                                 Identification division.                                      
                                 Program-ID.    MainPgm.                                       
                                                                                               
                                 Environment division.                                         
                                 Data division.                                                
                                 Working-storage section.                                      
                                                                                               
                                 Linkage section.                                              
                                                                                               
                                *--->copy qad0428cpy in qad.                                   
                                 01  Important-data Global.                                    
                                     02  Customer-number       pic x(8).                       
                                     02  Customer-name         pic x(20).                      
                                     02  Customer-shoe-size    pic s99v9      packed-decimal.  
                                     02  Customer-net-worth    pic s9(11)v99  packed-decimal.  
                          
                                                                                 
                                                                                 
                                 Procedure division using Important-data.        
                                 000-Main-logic.                                 
                                                                                 
                                     call "SUBPGM" using Important-Data          
                                                                                 
                                     goback.                                     
                                                                                 
                                 identification division.                        
                                 program-id.    SubPgm  initial common.          
                                 Environment division.                           
                                 Data division.                                  
                                 Working-storage section.                        
                                                                                 
                                *** what I would prefer to do                                 
                                 01  Hold-Shoe-size      like Customer-shoe-size.             
                                 01  New-net-worth       like Customer-net-worth.             
                                                                                              
                                 Linkage section.                                             
                                                                                              
                                *--->copy qad0428cpy in qad.                                  
                                 01  Important-data Global.                                   
                                     02  Customer-number       pic x(8).                      
                                     02  Customer-name         pic x(20).                     
                                     02  Customer-shoe-size    pic s99v9      packed-decimal. 
                                     02  Customer-net-worth    pic s9(11)v99  packed-decimal. 
                                                                                              
                                 Procedure division using Important-data.                     
                                 000-Sub-logic.                                               
                                                                                              
                                     move Customer-shoe-size to Hold-shoe-size                
                                     if Customer-shoe-size > 8.5                              
                                         continue                                             
                                     else                                                     
                                         continue                                             
                                     end-if                                                   
                                     move New-net-worth to Customer-net-worth                 
                                     exit program.                                            
                                                                                              
                                 end program SubPgm.                                          
                                 end program MainPgm.
                          Note that the GLOBAL declaration helps make this work. I did not test this to see what happens, but it compiles clean

                          Comment


                          • #14
                            Re: Like over linkage section data

                            Thanks for the great ideas, Tom and Terry. I admire your ingenuity!

                            In the interest of simplicity, I am going to forgo like and add a comment that the variable definitions must match the ones in the linkage section. Chances that any of these variable definitions will ever change are infinitesimal.

                            Comment


                            • #15
                              Re: Like over linkage section data

                              Originally posted by TedHolt View Post
                              Chances that any of these variable definitions will ever change are infinitesimal.
                              IOW,... next month? You're welcome.

                              BTW, I went ahead with a COPY in place and it worked as expected. Too bad something like malloc is needed, but at least it only requires the two special registers and the SPECIAL-NAMES entry.
                              Tom

                              There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                              Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                              Comment

                              Working...
                              X