如何在 COBOL 中对 X pic 子句执行算术

发布于 2024-11-02 11:33:27 字数 321 浏览 5 评论 0原文

我想在 COBOL 中编写一个函数,将字符从 PIC X(60) 数据类型转换为大写。虽然我知道一种方法,但我希望能够手动浏览字母并添加一个常量以从小写变为大写,但我似乎无法对 PIC X 数据类型执行任何算术,除非我将它们重新定义为其他东西,但随后我似乎破坏了原始的 X 字符。

有人可以给我一个例子,说明如何在给定 PIC X 变量的情况下将其增加 1 个字母(即 a -> b、d -> e...等)。换句话说,我希望能够为这个变量加 1:

     01 char-temp pic x.

谢谢,

Ivan

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... etc). In other words, I would like to be able to add 1 to this variable:

     01 char-temp pic x.

Thanks,

Ivan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2024-11-09 11:33:27

以这种方式进行大小写折叠通常是一个非常糟糕的主意,它实际上只适用于 7 位 ASCII。你问的是可能的,甚至很容易做到,但我会首先提到一些其他方法来大写你的数据。

最简单的方法:

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

非常简单的方法:

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

困难的方法,将 1 添加到 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

然后你可以:

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

并且 char-temp 将包含一个“B”。正如其他人提到的,Cobol 对于单字节二进制数学不太好,因此您必须使字符适合二进制数的低字节,在本例中是 2 字节二进制数。

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.

神魇的王 2024-11-09 11:33:27

你可以顽皮地使用重新定义,例如:

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

      move 65 to char-temp9
      display char-temp

或使用引用修改:

      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

我相信它们也是其他方法......

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

任谁 2024-11-09 11:33:27

单个字符通常占用一个字节的内存。标准 COBOL 没有单字节的二进制数值数据类型(注意:PIC 9 USAGE DISPLAY 是数字的字符表示形式,而不是数字的二进制表示形式)。因此,在 COBOL 中对单个字符进行二进制算术对您来说不起作用。

即使您可以对字符执行算术运算,您也可能对字符的二进制表示形式做出错误的假设 - 字母“b”不一定等于“a”加 1 的二进制表示形式(它可能适用于 ASCII,但不适用于 ASCII)对于 EBCDIC)。

最后,如果您想用另一个字符替换一个字符,您应该研究 COBOL INSPECT 语句。与位调整不同,INSPECT 是完全可移植的。

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.

栀子花开つ 2024-11-09 11:33:27

有人可以给我一个例子,说明如何在给定 PIC X 变量的情况下,您可以
将其增加 1 个字母(即 a -> b、d -> e...等)。换句话说,
我希望能够向该变量添加 1:

 01 字符临时图片 x。

对于 COBOL 85,可以使用内部函数 CHARORD

   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.

结果:

a->b

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文