Doug Hauf
Doug Hauf

Reputation: 3213

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.    

Upvotes: 1

Views: 4121

Answers (4)

Matthews
Matthews

Reputation: 11

I think this works? For some reason if VARIABLE = SPACES it is considered as numeric.

IF FUNCTION TRIM(VARIABLE) IS NOT = SPACES AND 
   FUNCTION TRIM(VARIABLE) IS NUMERIC      THEN
   DISPLAY 'NUMERIC'
   DISPLAY FUNCTION TRIM(VARIABLE)
ELSE
   DISPLAY 'NOT NUMERIC'
   DISPLAY FUNCTION TRIM(VARIABLE)
END-IF

The problem with this is MOVE '12.00' TO VARIABLE or MOVE '12,00' TO VARIABLE is recognized as NOT NUMERIC. I think only numbers that are BYTE, SMALLINT, INTEGER or LONG (no float or decimal) output it correcly.

Upvotes: 1

Zanayu
Zanayu

Reputation: 1

Recently had experienced non-numeric being successfully processed into a numeric (COMP-3) field.

Value of 'FALSE99999999' moved into a COMP-3 field and became 6132599999999 and the program never encounter an S0C7 ABEND.

Looks like verifying each byte of the source field is the only way to identify such issue.

Upvotes: 0

user3491862
user3491862

Reputation: 382

The following code example works and will check each position in you WORK-TRIG1 to see if that value is a NUMERIC. I tested this and it does work. This example uses a PERFORM VARYING loop to index through each "location" in the string to see if it is valid.

  Should Work ALSO:

        IF A IS NUMERIC THEN
           //code here
        END-IF

I do know that the below code works because I took it directly from a program that is running perfectly and has since 88 or 89.

Code:

CHECK-TRIG.                                                          
    PERFORM VARYING SUB1 FROM 1 BY 1 UNTIL SUB1 > 8                  
      IF WORK-TRIG(1:SUB1) IS NUMERIC THEN                           
         MOVE 'FALSE' TO ERR-TRIG                                    
      ELSE                                                           
         MOVE 'TRUE' TO ERR-TRIG                                     
         MOVE SUB1   TO SV-RTN-CODE 
         MOVE 9 TO SUB1                                 
      END-IF                                                         
    END-PERFORM.                                                     
X-CHECK. EXIT.        

--Code--New this does not work

PERFORM VARYING SUB1 FROM 1 BY 1 UNTIL SUB1 > 8          
  IF WORK-TRIG(SUB1:8) IS NUMERIC THEN                   
     MOVE 'FALSE' TO ERR-TRIG                            
  ELSE                                                   
     MOVE 'TRUE' TO ERR-TRIG                             
     MOVE SUB1   TO SV-RTN-CODE                          
     MOVE '   TRIGGER CARD ERROR (SEE DATE BELOW)' TO    
          ERR-DET                                        
     MOVE 9 TO SUB1                                      
  END-IF                                                 
END-PERFORM.                                              

Upvotes: 1

Bill Woodger
Bill Woodger

Reputation: 13076

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.

Upvotes: 2

Related Questions