且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用cobol计算变量的长度字符串

更新时间:2023-11-10 08:58:04

这是一种常见的方式:

MOVE ZERO TO count-of-trailing-spaces                                     

INSPECT FUNCTION REVERSE ( NOTE-TEXT )                       
   TALLYING count-of-trailing-spaces                                        
   FOR LEADING SPACE

SUBTRACT count-of-trailing-spaces                                     
  FROM LENGTH OF ( NOTE-TEXT )
  GIVING NOTE-LEN

功能反转会将字段的字节交换为反转订购。 INSPECT 没有 TALLYING ... TRAINING (某些供应商的编译器除外,但它是非标准的)因此 INSPECT ... LEADING ... 可以在字段反转后使用。

FUNCTION REVERSE will swap the bytes of a field into reverse order. INSPECT does not have TALLYING ... TRAILING (except in compilers from some vendors, but it is non-standard) so INSPECT ... LEADING ... can be used once the field is reversed.

有时候我应该考虑我的讽刺帽子掉了。如果使用功能反转,还请首先检查该字段的空间,那么反转500个空格然后计算500个前导空格是没有意义的。

Sometimes I should take my irony hat off. If using the FUNCTION REVERSE, also check the field for space first, there is no point in reversing 500 spaces and then counting 500 leading spaces.

也知道您的数据。如果音符大部分都很短,而您做的很多,则可能需要调查是否需要更快的速度。能否从中获得好处取决于您的数据和硬件,但请记住这一点。

Also "know your data". If notes are mostly short, and you do a lot of them, you might want to investigate whether something more speedy is required. It depends on your data and hardware as to whether there's a benefit to be had from that, but bear it in mind.

可能值得研究一下实际上知道字段的长度,并且已经可以告诉您。

It may be worth investigating whether something up the line actually knows how long the field is, and can already tell you.

我只是从后面循环,计算空格(第一个之后检查所有空间)。减轻CPU负担。做到这一点的一种方法:

I'd just loop from the back, counting spaces (after first checking for all space). Less strain on the CPU. One way to do that:

IF NOTE-TEXT EQUAL TO SPACE
    MOVE ZERO TO NOTE-LEN
ELSE
    MOVE LENGTH OF NOTE-TEXT TO NOTE-LEN
    PERFORM 
      UNTIL NOTE-TEXT-BYTE ( NOTE-LEN ) 
        NOT EQUAL TO SPACE
          SUBTRACT +1 FROM NOTE-LEN
    END-PERFORM
END-IF

这就需要定义 NOTE-TEXT-BYTE 作为 NOTE-TEXT 的组成部分。

Of course this requires a defintion of NOTE-TEXT-BYTE as being a constituent of NOTE-TEXT.

49级可能很重要,所以不能整齐地做:

The 49-level is probably significant, so can't do it so neatly:

49  NOTE-TEXT         PIC X(500).
49  NOTE-TEXT-BYTE
      REDEFINES NOTE-TEXT
      OCCURS 500      PIC X. 

也许49年代为参考修改提供了一些案例。也许不是。

Perhaps the 49s provide some case for reference-modification. Perhaps not.