开发者

How to perform arithmetic on X pic clauses in COBOL

I would like to write a function in COBOL that converts characters from a PIC X(60) data type to uppercase. While I know one way of doing this, I would like to be able to manually go through the letters and add a constant to go from lower case to upper case, but I cannot seem to be able to perform any arithmetic on PIC X data types, unless I redefine them as something else, but then I seem to corrupt the original X character.

Can someone give me an example of how, given a PIC X variable, you can increment it by 1 letter (i.e. a -> b, d -> e开发者_JAVA技巧... etc). In other words, I would like to be able to add 1 to this variable:

     01 char-temp pic x.

Thanks,

Ivan


It is usually a very bad idea to do case folding this way, it really only works for 7-bit ASCII. What you ask is possible, and even easy to do, but I'll mention some other ways to uppercase your data first.

Easiest way:

Move function upper-case(my-src-field) To my-tgt-field

Very easy way:

Inspect my-field 
   converting 'abc...z' to 'ABC...Z'
End-Inspect

Hard way, to add one to 01 char-temp pic x:

01 my-working-storage.
  02 char-temp-area.
    03 filler    pic x.
    03 char-temp pic x.
  02 char-temp-9 redefines char-temp-area pic s9(4) comp

.

Then you can:

Move 'A' to char-temp
Add +1 to char-temp-9

And char-temp will contain a 'B'. As others have mentioned, Cobol is not nice about single byte binary math, so you have to make your character fit in the low byte of a binary number, in this case a 2 byte binary number.


You could be naughty and use redefines eg:

      01 char-temp   pic x.
      01 char-temp9  pic 99 comp-x redefines char-temp.

      move 65 to char-temp9
      display char-temp

Or use reference modification:

      01 char-temp   pic x.
      01 char-temp9  pic 99 comp-x.

      move 65 to char-temp9
      move char-temp9(1:length of char-temp9) to char-temp

      display char-temp

and I am sure they are other approaches too...


A single character generally takes one byte of memory. Standard COBOL does not have a binary numeric data type of one byte (Note: PIC 9 USAGE DISPLAY is a character representation of a digit not the binary representation of the digit). So, doing binary arithemetic on single characters in COBOL isn't going to work for you.

Even if you could perform arithementic on characters, you may be making a bad assumption about the binary representation of characters - the letter 'b' is not necessarily equal to the binary representation of 'a' plus 1 (it might work for ASCII but not for EBCDIC).

Finally, if you want to replace one character by another you should investigate the COBOL INSPECT statement. Unlike bit twiddling, INSPECT is completely portable.


Can someone give me an example of how, given a PIC X variable, you can increment it by 1 letter (i.e. a -> b, d -> e... etc). In other words, I would like to be able to add 1 to this variable:

 01 char-temp pic x.

For COBOL 85, intrinsic functions CHAR and ORD may be used.

   program-id. add1.
   data division.
   working-storage section.
   1 char-temp pic x.
   procedure division.
   begin.
       move "a" to char-temp
       display char-temp "->" with no advancing
       perform add-1-to-char
       display char-temp
       stop run
       .

   add-1-to-char.
       move function char (function ord (char-temp) + 1)
           to char-temp
       .
   end program add1.

Result:

a->b
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜