Search code examples
cobol

Check a variable to make certain that it is all numbers?


How can I check Work-Trig to make sure that it is all digits?

code:

Work-Trig is --> 20140101

CHECK-TRIG.                          
    IF WORK-TRIG IS NUMERIC THEN     
       MOVE "FALSE" TO ERR-TRIG      
    ELSE                             
       MOVE "TRUE" TO ERR-TRIG       
    END-IF.                          
    DISPLAY 'ERR-TRIG' ERR-TRIG.     
X-CHECK. EXIT.  

01  WORK-TRIG.                               
 05 TRIG-YEAR  PIC X(08) VALUE SPACES.    
 05 TRIG-MONTH PIC X(01) VALUE SPACES.    
 05 TRIG-DAY   PIC X(01) VALUE SPACES.    
 05 FILLER     PIC X(70) VALUE SPACES.    

Solution

  • The problem is that WORK-TRIG is 80 bytes long. The first eight bytes contain your data, but the entire 80 bytes will be tested for being NUMERIC.

    You have a data-name for the first eight bytes. If you test that instead of the group-item, your code will work.

    CHECK-TRIG.                          
        IF WORK-YEAR IS NUMERIC THEN     
           MOVE "FALSE" TO ERR-TRIG      
        ELSE                             
           MOVE "TRUE" TO ERR-TRIG       
        END-IF.                          
        DISPLAY 'ERR-YEAR' ERR-TRIG.     
    X-CHECK. EXIT. 
    

    If you have a data-name called WORK-YEAR, it should only contain a year. It should not contain an entire date. The point of good names for data is so that we, humans, can read and understand your code better. When looking for a problem, we find WORK-YEAR as eight bytes long, and have to spend time finding out if that is the correct length, or the correct name and a wrong length.

    Given the code change, it would be good to use a different name for ERR-TRIG as well.

    There are more obscure ways to test the first eight bytes of a group item, but since you already had a name, hopefully we'll keep reference-modification out of this one.