COBOL Data Validation for capital letter?

喜你入骨 提交于 2019-12-23 13:32:34

问题


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.


回答1:


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!




回答2:


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.




回答3:


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.




回答4:


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').



来源:https://stackoverflow.com/questions/9012975/cobol-data-validation-for-capital-letter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!