Wednesday, December 21, 2011

COBOL : what exactly LOW-VALUES and HIGH-VALUES mean

First of all, what is a collating sequence? 
      Collating sequence is the logical ordering for the character set you are using. It is a sequence which defines the way characters may be ordered relative to other characters, i.e. the collating sequence defines the value and position of each character relative to other characters.

      For example, in the ASCII collating sequence, "1" < "A" < "a" (I just mean that the numbers occur before the capital letters, which in turn occur before the small letters), whereas in the EBCDIC collating sequence, "a" < "A" < "1". (The reason can be understood from the internal or HEX representation. In ASCII, "1" is X'31', "A" is X'41' and "a" is X'61', whereas in EBCDIC, "1" is X'F1', "A" is X'C1' and "a" is X'81'.)

What is "Program collating sequence"?
      Program collating sequence refers to the collating sequence that a program is using. The collating sequences allowed are:
1. STANDARD-1 or ASCII (7 bit character set - X'00' to X'7F')
2. STANDARD-2 (not very sure, but is said to be the International
              Reference Version of the ISO 7-bit code) 
3. NATIVE (the default. EBCDIC on mainframe and ASCII on windows)
4. EBCDIC (8 bit character set - X'00' to X'FF')
5. literal-1 literal-2 literal-3 (user-defined collating sequence)

High-values and low-values
      HIGH-VALUES and LOW-VALUES are figurative "constants". However, they may have different values in different programs, depending on the collating sequence the program uses. 

      As you have mentioned in your post, LOW-VALUES is defined as one or more of the character with the lowest position in the program collating sequence. HIGH-VALUES is defined as one or more of the character with the highest position in the program collating sequence. 

      Hence, to answer your question, for the EBCDIC collating sequence, LOW-VALUES has the value X"00" (an 8-bit character code of all binary zeros) and HIGH-VALUES has the value X"FF" (an 8-bit character code of all binary ones). Similarly, for the ASCII collating sequence, LOW-VALUES has the value X"00" (a 7-bit character code of all binary zeros) and HIGH-VALUES has the value X"7F" (a 7-bit character code of all binary ones).

How do you specify a collating sequence of your own?
      The program collating sequence may be specified by the PROGRAM COLLATING SEQUENCE clause in the OBJECT-COMPUTER paragraph using an alphabet-name defined in the SPECIAL-NAMES paragraph. Here is a simple way it can be done:

IDENTIFICATION DIVISION.                                  
PROGRAM-ID. TESTLVAL.                                      
ENVIRONMENT DIVISION.                                      
CONFIGURATION SECTION.                                    
OBJECT-COMPUTER. IBM                                      
       PROGRAM COLLATING SEQUENCE IS MY-COLLATE.          
SPECIAL-NAMES.                                            
       ALPHABET MY-COLLATE IS "ZYXWVUTSRQPONMLKJIHGFEDCBA".
INPUT-OUTPUT SECTION.                                      
FILE-CONTROL.                                              
DATA DIVISION.                                            
FILE SECTION.                                              
WORKING-STORAGE SECTION.                                  
                                                           
01  WS-FIELD1-ALPHA                        PIC X(01).      
                                                           
PROCEDURE DIVISION.                                        

     MOVE LOW-VALUES TO WS-FIELD1-ALPHA          
     DISPLAY 'LOW-VALUES IS  : ' WS-FIELD1-ALPHA
     MOVE HIGH-VALUES TO WS-FIELD1-ALPHA          
     DISPLAY 'HIGH-VALUES IS : ' WS-FIELD1-ALPHA
     STOP RUN.                                     

      Now what is the value of low-values displayed here? It is "Z"! And High-values is "A", as per the program collating sequence used here. If the collating sequence were EBCDIC, it would have been X'00' and X'FF' respectively.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.