如何在COBOL中将右边的空格替换为左边的零? [英] How to replace spaces at the right into zeros at the left in COBOL?

查看:340
本文介绍了如何在COBOL中将右边的空格替换为左边的零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度为10的字母数字变量,它的开头包含一个数字,其余数字均用空格填充.然后,我需要将字符串向左移动并将开头的空格数设为"0".这个例子说明了一切:

I have an alphanumeric variable with a length of 10. It contains a number at the beginning, the rest of the digits are filled with spaces. Then I need to move the string to the left and put the number of spaces with '0' at the begining. This examples speaks for themselves:

INPUT            OUTPUT
==============================
'123456    ' ->  '0000123456'
'12345678  ' ->  '0012345678'
'123456789 ' ->  '0123456789'
'1234567890' ->  '1234567890'

然后我努力做到这一点:

Then I tought in something like this:

检查此COBOL提琴,您可以在其中尝试: http://ideone.com/mgbKZ3 (只需单击 edit )

Check this COBOL fiddle where you can try: http://ideone.com/mgbKZ3 (just click on edit)

   IDENTIFICATION DIVISION.
   PROGRAM-ID. VARSWAP.
   ENVIRONMENT DIVISION.
   DATA DIVISION.
   WORKING-STORAGE SECTION. 

       01 VARIN   PIC X(10).
       01 VARSWAP PIC X(10) JUSTIFIED RIGHT.

   PROCEDURE DIVISION.

       MOVE '123456    ' TO VARIN

       UNSTRING VARIN DELIMITED BY ' ' INTO VARSWAP

       INSPECT VARSWAP REPLACING LEADING SPACE BY '0'

       MOVE VARSWAP TO VARIN

       DISPLAY VARIN

       STOP RUN.

返回:

0000123456

看来工作还可以,但是我想知道您是否有更好,更简单或更清晰的方法来实现这一目标.

It seems work ok, but I wonder if you have a better, simpler, or clearer way to do it.

推荐答案

您应该在输入所有空白的情况下测试代码.

You should test your code with an input of all blanks.

如果您对数据的质量 绝对是肯定的,并且有或没有空白检查,则可以执行以下操作:

If you are absolutely certain of the quality of the data, and with or without the check for blanks, you can do this:

   ID DIVISION.
   PROGRAM-ID. VARSWAP.
   DATA DIVISION.
   WORKING-STORAGE SECTION. 

       01 VARIN   PIC X(10).
          88  NO-VARIN-PRESENT VALUE SPACE.
       01 VARSWAP PIC 9(10).

   PROCEDURE DIVISION.

       MOVE '123456    ' TO VARIN
       IF NO-VARIN-PRESENT
           do what your spec says
       ELSE
           UNSTRING VARIN DELIMITED BY ' ' INTO VARSWAP
       END-IF

       DISPLAY VARSWAP

      GOBACK
      .

我不喜欢破坏输入,所以我改变了它.

I don't like destroying the input, so I changed that.

一种常见的实现方法是:FUNCTION REVERSE(您的字段),然后是INSPECT反向字段TALLYING ...以获得最佳空间.您可以在程序的早期使用FUNCTION LENGTH来确定字段的长度(并确保它们的长度相同),然后将VARIN首先设置为0,对源和目标使用引用修改-源将是( 1:计算长度数据的目标将是(计算右对齐的开始时间:)(未指定长度使用该字段的其余部分).

A popular way to do it is, FUNCTION REVERSE ( your-field ), followed by INSPECT reversed-field TALLYING ... FOR LEADING SPACES. You can use FUNCTION LENGTH early in your program to determine the length of the fields (and ensure they are the same length) and then, setting your VARIN to ZERO first, use reference-modification for the source and the target - source will be ( 1 : calculated-length-of-data ) target will be ( calculated-start-for-right-justification : ) (not specifying the length uses the remaining part of the field).

还有可变长度字段,逐字节MOVE(有时是传统主义者"偏爱的,但很少有人知道.)

There are also variable-length fields, byte-by-byte MOVEs (sometimes preferred by "traditionalists", but the least clear of the lot).

确切的操作方式取决于您的数据.如果需要验证数据,则首先需要代码,这将使您的选择更加明确.如果您的数据可以保证是干净的,那么...

Exactly how you do it depends on your data. If you need to validate the data, you need code for that first, and that will make the choice more clear to you. If your data is, guaranteed, clean, then...

我知道这只是一个例子,但我希望您使用更好的数据名称.

I know it is only an example, but I hope you use nicer data-names for real.

这篇关于如何在COBOL中将右边的空格替换为左边的零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆