如何将文本中的数字移动到数字?
我想问,如何将文本中的数字(例如: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不被禁止,
你只需要这样做
It is not forbidden
you just need to
COBOL 提供了几种完成此类任务的方法。开始于
您问题中概述的声明:
按照以下方式声明另一个数据项:
AAAABB
声明一个包含两个基本元素的记录结构(复合数据项)数据项:
AAAA
和BB
,两者都是数字。现在您可以执行以下任一操作以下:
显示的输出将为:
由于
AAAABB
是复合项,因此它具有PIC X
隐式数据类型。这反过来允许您分配几乎任何数据值,然后通过引用来分解它
到其各个组件。
请注意,如下的赋值:
这通常会编译(带有有关截断的警告)并生成以下内容
结果:
最高有效数字已被截断(可能不是您想要的)。
COBOL 在数据操作方面相当灵活。一
你应该注意(防范)的是非数字的分配
值到数字数据项,如下所示:
这将“工作”得很好,直到您尝试执行以下操作:
如果您幸运的话,此时它会爆炸(取决于您的编译器和
实际的非数字)。为了防止这种类型的错误,你应该始终
包括以下逻辑:
COBOL provides a few ways to accomplish this sort of assignment. Start with
the declarations outlined in your question:
Declare another data item along the lines of:
The
AAAABB
declares a record structure (compound data item) containing two elementarydata items:
AAAA
andBB
, both of which are numeric. Now you can do either of thefollowing:
The displayed output will be:
Since
AAAABB
is a compound item it has aPIC X
implicit data type. This in turnallows you to assign virtually any data value and then decompose it by referring
to its individual components.
Beware, an assignment such as:
This will generally compile (with warnings about truncation) and produce the following
result:
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:
This will "work" just fine until you try to do something like:
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:
您可以在 COBOL 中使用
redefines
进行联合。这是凭记忆,但我认为它应该有效:You can do unions in COBOL with
redefines
. This is from memory but I think it should work: