如何将文本中的数字移动到数字?

发布于 2024-10-21 15:19:06 字数 143 浏览 4 评论 0原文

我想问,如何将文本中的数字(例如:01 A PIC X(6) 值“200030”)移动到诸如 01 B PIC 9(6) 之类的数字,我只想提取前 4 个数字A. 在 Cobol 中,这种使用 MOVE 的移动是被禁止的,我使用的移动是 MOVE A(1:4) to B。

I would like to ask, how to move number in Text, e.g: 01 A PIC X(6) value "200030", to a number such as 01 B PIC 9(6) and I only want to extract the first 4 number of A. In Cobol this type of move using MOVE is forbidden, the move I used is MOVE A(1:4) to B.

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

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

发布评论

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

评论(3

像你 2024-10-28 15:19:06

这并不被禁止,

你只需要这样做

03  Field-x4              X(4).
03  Field-94              9(4).




  Move Field-X4            to Field-94

It is not forbidden

you just need to

03  Field-x4              X(4).
03  Field-94              9(4).




  Move Field-X4            to Field-94
白色秋天 2024-10-28 15:19:06

COBOL 提供了几种完成此类任务的方法。开始于
您问题中概述的声明:

  01 A PIC X(6) VALUE "200030".  
  01 B PIC 9(6).  

按照以下方式声明另一个数据项:

  01 AAAABB.   
     05 AAAA   PIC 9(4).   
     05 BB     PIC 9(2).   

AAAABB 声明一个包含两个基本元素的记录结构(复合数据项)
数据项:AAAABB,两者都是数字。现在您可以执行以下任一操作
以下:

  MOVE A(1:4) TO B  ; DISPLAY B
  MOVE A TO AAAABB  ; DISPLAY AAAA
                      DISPLAY BB

显示的输出将为:

002000  
2000
30  

由于 AAAABB 是复合项,因此它具有 PIC X 隐式数据类型。这反过来
允许您分配几乎任何数据值,然后通过引用来分解它
到其各个组件。

请注意,如下的赋值:

MOVE A TO AAAA; DISPLAY AAAA  

这通常会编译(带有有关截断的警告)并生成以下内容
结果:

0030    

最高有效数字已被截断(可能不是您想要的)。

COBOL 在数据操作方面相当灵活。一
你应该注意(防范)的是非数字的分配
值到数字数据项,如下所示:

MOVE "20++30" TO A
MOVE A TO AAAABB

这将“工作”得很好,直到您尝试执行以下操作:

ADD +1 TO AAAA

如果您幸运的话,此时它会爆炸(取决于您的编译器和
实际的非数字)。为了防止这种类型的错误,你应该始终
包括以下逻辑:

MOVE A TO AAAABB  
IF (AAAA NOT NUMERIC) OR (BB NOT NUMERIC)  
   PERFORM BAD-DATA-ASSIGNMENT  
END-IF  
ADD 1 TO AAAA  

COBOL provides a few ways to accomplish this sort of assignment. Start with
the declarations outlined in your question:

  01 A PIC X(6) VALUE "200030".  
  01 B PIC 9(6).  

Declare another data item along the lines of:

  01 AAAABB.   
     05 AAAA   PIC 9(4).   
     05 BB     PIC 9(2).   

The AAAABB declares a record structure (compound data item) containing two elementary
data items: AAAA and BB, both of which are numeric. Now you can do either of the
following:

  MOVE A(1:4) TO B  ; DISPLAY B
  MOVE A TO AAAABB  ; DISPLAY AAAA
                      DISPLAY BB

The displayed output will be:

002000  
2000
30  

Since AAAABB is a compound item it has a PIC X implicit data type. This in turn
allows you to assign virtually any data value and then decompose it by referring
to its individual components.

Beware, an assignment such as:

MOVE A TO AAAA; DISPLAY AAAA  

This will generally compile (with warnings about truncation) and produce the following
result:

0030    

The most significant digits have been truncated (probably not what you wanted).

COBOL is reasonably flexible with respect to data manipulation. One
thing you should be watching out for (guarding against) is assginment of non-numeric
values to numeric data items as in:

MOVE "20++30" TO A
MOVE A TO AAAABB

This will "work" just fine until you try to do something like:

ADD +1 TO AAAA

If you are lucky it will blow up at this point (depending on your compiler and
the actual non-numerics). To guard against this type of error you should always
include logic along the lines of:

MOVE A TO AAAABB  
IF (AAAA NOT NUMERIC) OR (BB NOT NUMERIC)  
   PERFORM BAD-DATA-ASSIGNMENT  
END-IF  
ADD 1 TO AAAA  
巾帼英雄 2024-10-28 15:19:06

您可以在 COBOL 中使用redefines 进行联合。这是凭记忆,但我认为它应该有效:

01  YEARMONTH.
    03  YM-FULL                     PIC 9(6).
    03  FILLER REDEFINES YM-FULL.
        05  YM-YEAR                 PIC 9(4).
        05  YM-MONTH                PIC 9(2).

01  JUST-YEAR                       PIC 9(4).

MOVE 200030 TO YM-FULL.
MOVE YM-YEAR TO JUST-YEAR.

You can do unions in COBOL with redefines. This is from memory but I think it should work:

01  YEARMONTH.
    03  YM-FULL                     PIC 9(6).
    03  FILLER REDEFINES YM-FULL.
        05  YM-YEAR                 PIC 9(4).
        05  YM-MONTH                PIC 9(2).

01  JUST-YEAR                       PIC 9(4).

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