Data Validation

让人想犯罪 __ 提交于 2019-12-11 14:44:49

问题


So I have entered my second semester of College and they have me doing a course called Advanced COBOL. As one of my assignments I have to my make a program that tests certain things in a file to make sure the input has no errors. I get the general idea but there are just a few things I don't understand and my teacher is one of those people who will give you an assignment and make you figure it out yourself with little or no help. So here is what I need help with.

  1. I have a field that the first 5 columns have to be numbers, the 6th column a capital letter and the last 2 numbers in a range of 01-68 or 78-99.

  2. one of my fields has to be a string of numbers with a dash in it like 00000-000, but some have more than one dash. How can I count the dashes to identify that there is a problem.


回答1:


Here are a few hints...

Use a hieratical record structure to view the data in different ways. For example:

 01 ITEM-REC.
    05 ITEM-CODE.
       10 ITEM-NUM-CODE    PIC 9(3).
       10 ITEM-CHAR-CODE   PIC A(3).
          88 ITEM-TYPE-A   VALUE 'AAA' THRU 'AZZ'.
          88 ITEM-TYPE-B   VALUE 'BAA' THRU 'BZZ'.
    05 QUANTITY            PIC 9(4).

ITEM-CODE is a 6 character group field, the first part of which is numeric (ITEM-NUM-CODE) and the last part is alphabetic (ITEM-CHAR-CODE). You can refer to any one of these three variables in your program. When you refer to ITEM-CODE, or any other group item, COBOL treats the variable as if it were declared as PIC X. This means you can MOVE just about anything into it without raising an error. For example:

MOVE 'ABCdef' TO ITEM-CODE

or

MOVE 'ABCdef0005' TO ITEM-REC

Neither one would cause an error even though the elementary data item ITEM-NUM-CODE is definitely not a number. To verify the validity of your data after a group move you should validate each elementary data item separately (unless you know for certain no data type errors could have occurred). There are a variety of ways to do this. For example if the data item has to be numeric the following would work:

IF ITEM-NUM-CODE IS NUMERIC
   CONTINUE
ELSE
   DISPLAY 'ITEM-NUM-CODE IS NOT NUMERIC'
   PERFORM BIG-BAD-ERROR
END-IF

COBOL provides various class tests which can be applied against a data item. For example: NUMERIC, ALPHABETIC and ALPHANUMERIC are commonly used.

Another common way to test for ranges of values is by defining various 88 levels - but exercise caution. In the above example ITEM-TYPE-A is an 88 level that defines a data range from 'AAA' through 'AZZ' based on the collating sequence currently in effect. To verify that ITEM-CHAR-CODE contains only alphabetic characters and the first letter is an 'A' or a 'B', you could do something like:

IF ITEM-CHAR-CODE ALPHABETIC
   DISPLAY 'ITEM-CHAR-CODE is alphabetic.'
   EVALUATE TRUE
      WHEN ITEM-TYPE-A
         DISPLAY 'ITEM-CHAR-CODE is in range AAA through AZZ'
      WHEN ITEM-TYPE-B
         DISPLAY 'ITEM-CHAR-CODE is in range BAA through BZZ'
      WHEN OTHER
         DISPLAY 'ITEM-CHAR-CODE is in some other range'
   END-EVALUATE
ELSE
   DISPLAY 'ITEM-CHAR-CODE is not alphabetic'
END-IF        

Note the separate test for ALPHABETIC above. Why do that when the 88 level tests could have done the job? Actually the 88's are not sufficient because they cover the entire range from AAA through AZZ based on the collating sequence currently in effect. In an EBCDIC based environment (a very large number of COBOL shops use EBCDIC) this captures values such as A}\. the close-brace and backslash characters are non-alpha but fall into the middle of the range 'A' through 'Z' (what the #*@! is that all about?). Also note that a value such as 'aaa' would not satisfy the ITEM-TYPE-A condition because lower case letters fall outside the defined range. Maybe time to check out an EBCDIC character table.

Finally, you can count the number of occurrences of a character, or string of characters, in a variable with the INSPECT verb as follows:

INSPECT ITEM-CODE TALLING DASH-COUNT FOR ALL '-'

DASH-COUNT needs to be a numeric item and will contain the number of dash characters in ITEM-CODE. The INSPECT verb is not so useful if you want to count the number of digits. For this you would need one statement for each digit.

It might be easier to just code a loop something like:

PERFORM VARYING I FROM 1 BY 1
           UNTIL I > LENGTH OF ITEM-CODE
   EVALUATE ITEM-CODE(I:1)
      WHEN '-'
         COMPUTE DASH-COUNT = DASH-COUNT + 1
      WHEN '0' THRU '9'
         COMPUTE DIGIT-COUNT = DIGIT-COUNT + 1
      WHEN OTHER
         COMPUTE OTHER-COUNT = OTHER-COUNT + 1
   END-EVALUATE
END-PERFORM

Now ask yourself why I was comfortable using a zero through 9 range check? Hint: look at the collating sequence.

Hope this helps.



来源:https://stackoverflow.com/questions/4719008/data-validation

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