Monday, July 25, 2011

SWAPBAR

Have you ever felt bored by using the old screen swapping methods in mainframe(Swap n/n+F9/SWAP)?  or have you ever wonder why IBM cannot make something similar to multi-tabs in your favorite web-browser? well the answer is yes we do have something similar to that.

When IBM released z/OS 1.10 back in 2009, they introduced a new ISPF keyword called “SWAPBAR”, which will display all the active logical session names at bottom of the screen, these session names will be POINT-AND- SHOOT enabled; so that you can simply point your cursor to the session name which you want to pull up and then press the Enter key. Entry for your current session will have an asterisk ( * ) at the first character’s position. If the list is bigger than the width of your 3270 screen then a “>” or “<” will be displayed, you can then scroll through the list by pointing your cursor on “<” or “>” and hit Enter or hit F10/F11.

You can turn SWAPBAR off by entering “SWAPBAR OFF”.

image

image

Saturday, July 16, 2011

Demystifying COMMAREA

Well COMMAREA its not a mystery anymore, it’s a very simple, convenient method to transfer data in CICS environment. You can use COMMAREA in a RETUN, XCTL or LINK. Called program can alter the data in the passed COMMAREA, then it will be passed to caller when called program issue a RETURN.

How to use  COMMAREA in CICS Program.

We must declare the commarea structure in the working storage section of the program which we are planning to use commarea. Then the same must be declared under DFHCOMMAREA group element in Linkage section of the program.  In the called Program the commarea must be defined as the first item in the Linkage section.

e.g.

Working-storage section.                                                         
01  WS-COMMAREA.                                                        
    03  WS-TRANSID              PIC X(4).

………

Linkage section.                                                        
01 dfhcommarea.                                                         
    03  ls-commarea             pic x(4).

When we use the commarea in a XCTL or a LINK the length of COMMAREA is mandatory. the length data name should be defined as half word binary “ S9(4) comp.  When we use COMMAREA in a CICS program, we can track the length of the COMMAREA by using EIBCALEN. This technique is widely used for PSUDO-CONVERSATIONAL programing (I’ll be blogging about PSEUDO-CONVERSATIONAL programming in coming days-please do-visit again).

LIMITATION and ALTERNATIVE TECHNOLOGIES

COMMAREA is as old as CICS so obviously it may-not be a wise choice for todays-programming. By the word todays-programs I’m referring to web-Mainframe-XML programs, because I have seen a lot of applications which gets hell lot of data as XML from java/WEB applications.

Maximum amount of data that a COMMAREA can hold is 32 Kilobytes. I you want to use more than this you can go ahead with using a data set ( Obviously it was an old alter native) or you can use CHANNELS and containers where the limit is the systems limit. ‘ll be blogging about channels and containers in the coming days.

sample program

This is a sample PSEUDO-CONVERSATIONAL CICS program, which is utilizing COMMAREA. Note the lines which in Blue color and BOLD- these are the key-part of our program.

Identification division.
program-id.    pseu01.
data division.
working-storage section.
01  ws-commarea                pic x(4).

01  ws-msg-o.
     05  msg-o-data             pic x(35).
77  msg-len                    pic s9(4) comp.
01  ws-msg-2.
     05  msg-1                  pic x(6).
     05  name                   pic x(5).
     05  msg-2                  pic x(17).
linkage section.
01  dfhcommarea.
     05  ls-trnid               pic x(4).

procedure division.
     exec cics handle condition
       lengerr(TSK-ERR-RTN)
     end-exec.
     if eibcalen = 0 then
       go to TSK1-RTN.
     if ls-trnid = 'TSK2' then
       go to TSK2-RTN
     end-if.
TSK1-RTN.
     move 'Enter Your name:' to msg-o-data.
     move 16 to msg-len.
     exec cics send
       from(msg-o-data)
       length(msg-len)
       erase
     end-exec.
     Move 'TSK2' to ws-commarea.
     exec cics return
       transid('TSK1')
       commarea(ws-commarea)
      length(4)
    end-exec.
TSK2-RTN.
    exec cics receive
      into(name)
      length(msg-len)
    end-exec.
    move 'Hello,' to msg-1.
    move ' welcome to CICS.' to msg-2
    move 28 to msg-len.
    exec cics send
      from(ws-msg-2)
      length(msg-len)
      erase
    end-exec.
    exec cics return
    end-exec.
TSK-ERR-RTN.
    move low-values to msg-o-data.
    move 'something goes wrong with leng.' to msg-o-data.
    move 32 to msg-len.
    exec cics send
      from(msg-o-data)
      length(msg-len)
      erase
    end-exec.
    exec cics return
    end-exec.

Sunday, July 3, 2011

SORTING A TABLE in COBOL

Hi folks, I could see the folds on you forehead. I can understand what you are thinking while reading the heading “Sorting table! its not required because we have sort utilities to do this”. But trust me there can be situations in CICS-COBOL program. Few days back one of my friend contacted me to get the code for sorting a table in Cobol, his requirement was very rare/unique so I had to write custom sort for his requirement, since he needs it in CICS-COBOL environment we could not use SORT verb. The reason why I said the story is, before started with writing the code I searched a couple of times in internet to see some sample codes; but internet disappointed me I couldn’t very much useful codes over there. I thought I can implement a simple “bubble sort” to sort the table.

How Bubble sort works?

The algorithm take elements from left most node to right most node and compare two adjacent elements and replace their positions if the right most element is greater than left most element of the pair.

lets take an example of  array {5 1 4 2 8}
The algorithm goes for n (length of array) n iterations across the array, and take one element in each phase
First Pass:
( 5 1 4 2 8 ) > ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) > ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) > ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) > ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) > ( 1 4 2 5 8 )
( 1 4 2 5 8 ) > ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
( 1 2 4 5 8 ) > ( 1 2 4 5 8 )
the array is sorted, and the algorithm can terminate. Please see the below graphical illustration!.

Bubble-sort-example-300px (Image courtesy Wikipedia)

COBOL program for BUBBLE SORT (COBOL program for sorting an array)

 

IDENTIFICATION DIVISION.                                    
PROGRAM-ID. SORT01.                                         
AUTHOR.     SHIBU.T.                                        
DATA DIVISION.                                              
WORKING-STORAGE SECTION.                                    
01  TBL.                                                    
    02 WS-TBL OCCURS 10.                                    
       05  WS-FLD    PIC 99.                                
       05  WS-FLD1   PIC X(3).                              
       05  WS-FLD2   PIC 99.                                
01  WS-TAB-HLD.                                             
       05  WK-FLD    PIC 99.                                
       05  WK-FLD1   PIC X(3).                              
       05  WK-FLD2   PIC 99.                                
01  WS-I             PIC 99.                                
01  WS-J             PIC 99.                                
01  K                PIC 99.                                
PROCEDURE DIVISION.                                         
    MOVE '01AAA25'    TO WS-TBL(1)                          
    MOVE '01BBB20'    TO WS-TBL(2)                          
    MOVE '04CCC26'    TO WS-TBL(3)                          
    MOVE '01DDD10'    TO WS-TBL(4)                          
    MOVE '05EEE26'    TO WS-TBL(5)                          
    MOVE '04FFF30'    TO WS-TBL(6)                          
    DISPLAY '>>>>>>>>BEFORE SORT<<<<<<<<'                   
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6         
      DISPLAY WS-TBL(WS-I)                                  
    END-PERFORM.                                            
    DISPLAY '>>>>>>>>ASCENDING ORDER<<<'                    
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I = 7         
       PERFORM VARYING WS-J FROM WS-I BY 1 UNTIL WS-J > 6   
            IF WS-FLD(WS-J) < WS-FLD(WS-I) THEN             
                 MOVE WS-TBL(WS-I)  TO WS-TAB-HLD           
                 MOVE WS-TBL(WS-J)  TO WS-TBL(WS-I)         
                 MOVE WS-TAB-HLD TO WS-TBL(WS-J)            
            END-IF                                          
       END-PERFORM                                          
   END-PERFORM.                                              
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6           
     DISPLAY WS-TBL(WS-I)                                    
   END-PERFORM.                                              
   DISPLAY '>>>>>>>>DESCENDING ORDER<<<<<<'                  
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I = 7           
      PERFORM VARYING WS-J FROM WS-I BY 1 UNTIL WS-J > 6     
           IF WS-FLD(WS-J) > WS-FLD(WS-I) THEN               
                MOVE WS-TBL(WS-I)  TO WS-TAB-HLD             
                MOVE WS-TBL(WS-J)  TO WS-TBL(WS-I)           
                MOVE WS-TAB-HLD TO WS-TBL(WS-J)              
           END-IF                                            
      END-PERFORM                                            
   END-PERFORM.                                              
   PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6           
     DISPLAY WS-TBL(WS-I)                                    
   END-PERFORM.                                              
   STOP RUN. 

IF WS-FLD(WS-J) < WS-FLD(WS-I) THEN  this line really defines the type of the sort, the above code makes sort to “sort in ascending order”. if you change the above line to IF WS-FLD(WS-J) > WS-FLD(WS-I) THEN Sort will turn as a " Sort in Descending order"

Output.

Please note, we are taking only first two digits as key.

image