用cobol计算变量的长度字符串 [英] compute length string of variable with cobol

查看:434
本文介绍了用cobol计算变量的长度字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库表
中有一个注释如何计算该字符串的长度?

I have a NOTE in database table How can i calculate the length of that string?

我有一个定义为

10 NOTE.
    49 NOTE-LEN          PIC S9(4) USAGE COMP.
    49 NOTE-TEXT         PIC X(500).

注是一个由500个字符组成的字符串。
我想计算音符长度。

Note is a string of 500 characters. I want to compute the note length.

推荐答案

这是一种常见的方式:

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.

这篇关于用cobol计算变量的长度字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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