COBOL 替代 BASIC 的 MID 以及如何连接字符串?

发布于 2024-09-10 10:54:10 字数 129 浏览 6 评论 0原文

我正在寻找 Visual Basic 的 MID 函数的 COBOL 替代方案。我需要做的就是从 8 个字符串中取出前 5 个字母并将它们连接起来。

我正在使用富士通COBOL。

非常感谢,

伊万

I'm looking for the COBOL alternative of Visual Basic's MID Function. The thing I need to do is take from 8 strings the first 5 letters and concatenate them.

I'm using Fujitsu COBOL.

Many thanks,

Yvan

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

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

发布评论

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

评论(4

骄傲 2024-09-17 10:54:10

Paxdiablo 给出了一些有效的方法来做到这一点。另一种方法是除了 STRING 动词之外还使用引用修饰。完整的程序示例如下:

   IDENTIFICATION DIVISION.                                      
   PROGRAM-ID. EXAMPLE9.                                         
   DATA DIVISION.                                                
   WORKING-STORAGE SECTION.                                      
   01  STRINGS.                                                  
       05 STRING1    PIC X(10) VALUE 'AAAAAAAAAA'.               
       05 STRING2    PIC X(10) VALUE 'BBBBBBBBBB'.               
       05 STRING3    PIC X(10) VALUE 'CCCCCCCCCC'.               
       05 STRING4    PIC X(10) VALUE 'DDDDDDDDDD'.               
       05 STRING5    PIC X(10) VALUE 'EEEEEEEEEE'.               
       05 STRING6    PIC X(10) VALUE 'FFFFFFFFFF'.               
       05 STRING7    PIC X(10) VALUE 'GGGGGGGGGG'.               
       05 STRING8    PIC X(10) VALUE 'HHHHHHHHHH'.               
       05 STRING-OUT PIC X(40) VALUE SPACES.                     
   PROCEDURE DIVISION.

       STRING STRING1(1:5) STRING2(1:5) STRING3(1:5) STRING4(1:5)
              STRING5(1:5) STRING6(1:5) STRING7(1:5) STRING8(1:5) 
              DELIMITED BY SIZE                                   
         INTO STRING-OUT

       DISPLAY STRING-OUT                                         
       GOBACK.                                                    

这大大减少了冗长,并在单个语句中捕获了串联。最好的建议是阅读STRING动词。它有多种创新的使用方式。

COBOL 没有提供与 BASIC MID 语句完全相同的模拟。您可以通过使用 STRINGUNSTRINGINSPECT 和引用修改的某种组合来完成类似的操作。引用修改的一个示例是:SOME-VARIABLE-NAME(1:5) - 1:5 位指定以 SOME-VARIABLE-NAME 开头的子字符串第一个字符的长度为 5 个字符。修饰符本身可以是数值变量。 STRINGUNSTRING 动词提供了许多非常强大的功能。

但总的来说,COBOL 并不是特别擅长字符串操作(有些人可能会说它在任何方面都不是特别擅长 - 但我不同意这种说法)。

Paxdiablo has given a couple of valid ways to do it. Another way would be to use reference modification in addition to the STRING verb. Complete program example follows:

   IDENTIFICATION DIVISION.                                      
   PROGRAM-ID. EXAMPLE9.                                         
   DATA DIVISION.                                                
   WORKING-STORAGE SECTION.                                      
   01  STRINGS.                                                  
       05 STRING1    PIC X(10) VALUE 'AAAAAAAAAA'.               
       05 STRING2    PIC X(10) VALUE 'BBBBBBBBBB'.               
       05 STRING3    PIC X(10) VALUE 'CCCCCCCCCC'.               
       05 STRING4    PIC X(10) VALUE 'DDDDDDDDDD'.               
       05 STRING5    PIC X(10) VALUE 'EEEEEEEEEE'.               
       05 STRING6    PIC X(10) VALUE 'FFFFFFFFFF'.               
       05 STRING7    PIC X(10) VALUE 'GGGGGGGGGG'.               
       05 STRING8    PIC X(10) VALUE 'HHHHHHHHHH'.               
       05 STRING-OUT PIC X(40) VALUE SPACES.                     
   PROCEDURE DIVISION.

       STRING STRING1(1:5) STRING2(1:5) STRING3(1:5) STRING4(1:5)
              STRING5(1:5) STRING6(1:5) STRING7(1:5) STRING8(1:5) 
              DELIMITED BY SIZE                                   
         INTO STRING-OUT

       DISPLAY STRING-OUT                                         
       GOBACK.                                                    

This cuts down on the verbosity quite a bit and captures the concatenation in a single statement. Best advice is to read up on the STRING verb. There are a number of innovative ways it can be used.

COBOL does not provide an exact analogue to the BASIC MID statement. You can accomplish similar operations by using some combination of STRING, UNSTRING, INSPECT and reference modification. An example of a reference modification is: SOME-VARIABLE-NAME(1:5) - the 1:5 bit specifies a substring of SOME-VARIABLE-NAME starting with the first character for a length of 5 characters. The modifiers may themselves be numeric variables. The STRING and UNSTRING verbs provide a number of features that can be quite powerful.

In general though, COBOL is not particularly good at string manipulation (some might say its not particularly good at anything - but I would disagree with that statement).

尝蛊 2024-09-17 10:54:10

我认为事情是这样的:

WORKING STORAGE SECTION.
    01  NORMAL-STRING-A    PIC X(80)
    01  NORMAL-STRING-B    PIC X(80)
    01  NORMAL-STRING-C    PIC X(80)
    01  NORMAL-STRING-D    PIC X(80)
    01  NORMAL-STRING-E    PIC X(80)
    01  SUB-STRING.
        05  FIVE           PIC X(5)
        05  REST           PIC X(75)
    01  TWENTY-FIVE-A.
        05  FIVE-A         PIC X(5).
        05  FIVE-B         PIC X(5).
        05  FIVE-C         PIC X(5).
        05  FIVE-D         PIC X(5).
        05  FIVE-E         PIC X(5).
    01  TWENTY-FIVE-B      PIC X(25).

PROCEDURE DIVISION.
    MOVE NORMAL-STRING-A TO SUB-STRING.
    MOVE FIVE TO FIVE-A.

    MOVE NORMAL-STRING-B TO SUB-STRING.
    MOVE FIVE TO FIVE-B.

    MOVE NORMAL-STRING-C TO SUB-STRING.
    MOVE FIVE TO FIVE-C.

    MOVE NORMAL-STRING-D TO SUB-STRING.
    MOVE FIVE TO FIVE-D.

    MOVE NORMAL-STRING-E TO SUB-STRING.
    MOVE FIVE TO FIVE-E.

    MOVE TWENTY-FIVE-A TO TWENTY-FIVE-B.

然后,你的字符串位于 TWENTY-FIVE-B 中。

你知道,我无法想象为什么人们认为 COBOL 很冗长:-)


更严重的是,我认为你可以沿着这些思路做一些事情来达到相同的结果(你可能必须摆弄开始和长度参数,我已经有一段时间没有认真做过 COBOL 了):

WORKING STORAGE SECTION.
    01  STRING-A           PIC X(80)
    01  STRING-B           PIC X(80)
    01  STRING-C           PIC X(80)
    01  STRING-D           PIC X(80)
    01  STRING-E           PIC X(80)
    01  TWENTY-FIVE        PIC X(25).

PROCEDURE DIVISION.
    MOVE STRING-A(1:5) TO TWENTY-FIVE( 1:5).
    MOVE STRING-B(1:5) TO TWENTY-FIVE( 6:5).
    MOVE STRING-C(1:5) TO TWENTY-FIVE(11:5).
    MOVE STRING-D(1:5) TO TWENTY-FIVE(16:5).
    MOVE STRING-E(1:5) TO TWENTY-FIVE(21:5).

I think it goes something like this:

WORKING STORAGE SECTION.
    01  NORMAL-STRING-A    PIC X(80)
    01  NORMAL-STRING-B    PIC X(80)
    01  NORMAL-STRING-C    PIC X(80)
    01  NORMAL-STRING-D    PIC X(80)
    01  NORMAL-STRING-E    PIC X(80)
    01  SUB-STRING.
        05  FIVE           PIC X(5)
        05  REST           PIC X(75)
    01  TWENTY-FIVE-A.
        05  FIVE-A         PIC X(5).
        05  FIVE-B         PIC X(5).
        05  FIVE-C         PIC X(5).
        05  FIVE-D         PIC X(5).
        05  FIVE-E         PIC X(5).
    01  TWENTY-FIVE-B      PIC X(25).

PROCEDURE DIVISION.
    MOVE NORMAL-STRING-A TO SUB-STRING.
    MOVE FIVE TO FIVE-A.

    MOVE NORMAL-STRING-B TO SUB-STRING.
    MOVE FIVE TO FIVE-B.

    MOVE NORMAL-STRING-C TO SUB-STRING.
    MOVE FIVE TO FIVE-C.

    MOVE NORMAL-STRING-D TO SUB-STRING.
    MOVE FIVE TO FIVE-D.

    MOVE NORMAL-STRING-E TO SUB-STRING.
    MOVE FIVE TO FIVE-E.

    MOVE TWENTY-FIVE-A TO TWENTY-FIVE-B.

Then, your string is in TWENTY-FIVE-B.

You know, I can't imagine why people thought COBOL was verbose :-)


On a more serious note, I think you can do something along these lines to achieve the same result (you may have to fiddle with the start and length parameters, it's been a while since I did any serious COBOL):

WORKING STORAGE SECTION.
    01  STRING-A           PIC X(80)
    01  STRING-B           PIC X(80)
    01  STRING-C           PIC X(80)
    01  STRING-D           PIC X(80)
    01  STRING-E           PIC X(80)
    01  TWENTY-FIVE        PIC X(25).

PROCEDURE DIVISION.
    MOVE STRING-A(1:5) TO TWENTY-FIVE( 1:5).
    MOVE STRING-B(1:5) TO TWENTY-FIVE( 6:5).
    MOVE STRING-C(1:5) TO TWENTY-FIVE(11:5).
    MOVE STRING-D(1:5) TO TWENTY-FIVE(16:5).
    MOVE STRING-E(1:5) TO TWENTY-FIVE(21:5).
白龙吟 2024-09-17 10:54:10

子字符串示例页面显示了一些变化。一个例子:

*   Task3 suffix(xStr,Length)
*   Extract the last Length number of chars from a string
*   Solution - use reference modification with start of substring
*   defined as the FullStringLength - SubStringLength + 1
*   In this example we get the last 13 characters.
MOVE 13 TO StrLength
DISPLAY "Task3 = " xStr2((StrSize - StrLength) + 1:StrLength)

This substring examples page shows a few variations. An example:

*   Task3 suffix(xStr,Length)
*   Extract the last Length number of chars from a string
*   Solution - use reference modification with start of substring
*   defined as the FullStringLength - SubStringLength + 1
*   In this example we get the last 13 characters.
MOVE 13 TO StrLength
DISPLAY "Task3 = " xStr2((StrSize - StrLength) + 1:StrLength)
糖粟与秋泊 2024-09-17 10:54:10
WORKING STORAGE SECTION.
01  NORMAL-STRING-A    PIC X(80)
01  NORMAL-STRING-B    PIC X(80)
01  NORMAL-STRING-C    PIC X(80)
01  NORMAL-STRING-D    PIC X(80)
01  NORMAL-STRING-E    PIC X(80)
01  TWENTY-FIVE-A.
    05  FIVE-A         PIC X(5).
    05  FIVE-B         PIC X(5).
    05  FIVE-C         PIC X(5).
    05  FIVE-D         PIC X(5).
    05  FIVE-E         PIC X(5).
01  TWENTY-FIVE-B REDEFINES TWENTY-FIVE-A PIC X(25).

PROCEDURE DIVISION.
    MOVE NORMAL-STRING-A TO FIVE-A
    MOVE NORMAL-STRING-B TO FIVE-B
    MOVE NORMAL-STRING-C TO FIVE-C
    MOVE NORMAL-STRING-D TO FIVE-D
    MOVE NORMAL-STRING-E TO FIVE-E
WORKING STORAGE SECTION.
01  NORMAL-STRING-A    PIC X(80)
01  NORMAL-STRING-B    PIC X(80)
01  NORMAL-STRING-C    PIC X(80)
01  NORMAL-STRING-D    PIC X(80)
01  NORMAL-STRING-E    PIC X(80)
01  TWENTY-FIVE-A.
    05  FIVE-A         PIC X(5).
    05  FIVE-B         PIC X(5).
    05  FIVE-C         PIC X(5).
    05  FIVE-D         PIC X(5).
    05  FIVE-E         PIC X(5).
01  TWENTY-FIVE-B REDEFINES TWENTY-FIVE-A PIC X(25).

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