4

I'm in my second quarter of college and taking "Advanced COBOL" we just received an assignment that requires us to code in some validation procedures for different data. I have everything done except on small validation procedure.

There is a field called "PART-NUMBER" that is 8 bytes long. The first 5 columns must be a number. The 6th column must be a capital letter and the last 2 columns must be in the range of 01-68 or 78-99. The only problem I have is figuring out how to validate that the 6th column is capital.

Here is the code I am using: From working storage:

01 DETAIL-LINE.                                         
05 PART-NUMBER.                                     
    10 PART-FIRST-FIVE-DL        PIC X(5).          
    10 PART-LETTER-DL            PIC X.             
        88 CAPITAL-LETTER        VALUE 'A' THRU 'Z'.
    10 PART-LAST-TWO-DL          PIC XX.

From 300-VALIDATE-PART-NUMBER

EVALUATE PART-LETTER-DL ALPHABETIC               
   WHEN TRUE EVALUATE CAPITAL-LETTER               
      WHEN FALSE MOVE 'YES' TO RECORD-ERROR-SWITCH
      MOVE 'PART NUMBER' TO FIELD-NAME            
      MOVE PART-NO-IN TO FIELD-VALUE              
      MOVE 'YES' TO PART-NO-ERROR                 
   END-EVALUATE                                    
   WHEN FALSE MOVE 'YES' TO RECORD-ERROR-SWITCH    
   MOVE 'PART NUMBER' TO FIELD-NAME                
   MOVE PART-NO-IN TO FIELD-VALUE                  
   MOVE 'YES' TO PART-NO-ERROR                     
END-EVALUATE 

I know I'm probably not doing this in a very efficient way but for now I just need to get it to work. I've read the whole chapter on data validation from the book and this is sort of a last minute error (program is due tomorrow) so the teacher is unavailable. I would greatly appreciate any help I can get with this. I'm really lost on how I'm supposed to validate capital letters. The method I'm using now reports an error if anything other than A or Z is in the 6th column of the part number.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Shane
  • 89
  • 2
  • 9
  • Someone last year had the same problem, but didn't make it as far as you did -- [Data Validation (COBOL)](http://stackoverflow.com/questions/4719008/data-validation-cobol) -- but the answer there is nice. – sarnold Jan 26 '12 at 02:21
  • Yeah, I actually found that before I posted my question but I didn't understand it at all. I think it's way beyond what we've been taught so far. – Shane Jan 26 '12 at 02:34

4 Answers4

4

I don't see anything fundamentally wrong with your code. I put it into a driver program, compiled and ran it. I got the expected results: Error reported only when the 6th character of PART-NUMBER was not an upper case letter.

Your COBOL coding style is very different from what I am used to seeing (not wrong, just different).

Most veteran COBOL programmers would code something like:

    IF PART-LETTER-DL IS ALPHABETIC AND
       CAPITAL-LETTER
       CONTINUE
    ELSE
       MOVE 'PART NUMBER' TO FIELD-NAME            
       MOVE PART-NO-IN TO FIELD-VALUE              
       MOVE 'YES' TO PART-NO-ERROR
    END-IF

The IF applies both of your edit criteria and does nothing if both pass (CONTINUE), otherwise an error is reported (ELSE part). The above does essentially the same thing your code example does except using IF as opposed to EVALUATE.

I give you full marks for testing both ALPHABETIC and capital letter using an 88 level range (THRU). A lot of programmers would only use the 88 level, making the implicit assumption that 'A' THRU 'Z' covers only alphabetic characters - this is dead wrong in some environments (EBCDIC character sets in particular).

P.S. I see you guys must have the same teacher that Kimmy had!

NealB
  • 16,670
  • 2
  • 39
  • 60
1

For capital letters you can test the ALPHABETIC-UPPER condition:

IF PART-LETTER-DL NOT EQUAL SPACE AND PART-LETTER-DL IS ALPHABETIC-UPPER
 ...
END-IF.

ALPHABETIC-LOWER can be used too, but remember that SPACE is considered ALPHABETIC, so testing SPACE is necessary, if you just want capital letters.

1

One thing you should be concerned about is the "Value 'A' thru 'Z'". It will only work on ASCII machines.

If you actually code Value 'A', 'B', 'C', ... 'Z'. It will work on all platforms.

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
  • The code as presented in the question will work properly on all platforms I can think of. It would only fail were a character set encoding mixed upper/lower case letters within the same value range. This doesn't happen in ASCII, EBCDIC or Unicode character sets so I figure the approach taken was pretty much bullet proof. However, `'A' THRU 'Z'` without the `ALPHABETIC` test would fail in EBCDIC because non alphabetic characters occur within the 'A' through 'Z' value range. – NealB Jan 27 '12 at 18:33
  • "A" thru "Z" would allow non characters to be valid...On EBCDIC. – Joe Zitzelberger Jan 27 '12 at 20:12
  • ... but ALPHABETIC does not - putting the two tests together solves this problem – NealB Jan 27 '12 at 20:14
  • True for the code as written. But the data item still leaves the door open to be misinterpreted. – Joe Zitzelberger Jan 27 '12 at 20:39
0

For EBCDIC, drop the ALPHABETIC test and just use the 88:

88 CAPITAL-LETTER        VALUE 'A' THRU 'I'
                               'J' THRU 'R'
                               'S' THRU 'Z'.

Specifying individual letters works, but generates 26 comparisons! The above generates three. The ALPHABETIC plus 'A' THRU 'Z' only two, but does carry some in-built confusion (space is alphabetic, and the THRU includes non-printable digits in the range X'C1' to X'E9').

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47