所以我的输入文件会是这样的:
words words nothing important
words words nothing important
the quick brown fox 23
the quick brown fox 14
words words nothing important
words words nothing important
现在,我希望能够抓住第一个“狐狸”实例并在WS-FIRST中捕获“23”然后抓住第二个“狐狸”实例并在WS-SECOND中捕获“14”。
我将用不同的字符串替换“快速棕色狐狸”,但两行都是一样的,所以很容易。
文本在内容中固定并固定在位,数字也在内容,位置和长度上固定。
答案 0 :(得分:1)
01 field-we-are-about-to-change.
05 FILLER.
10 the-bit-you-want-to-change PIC X(length of that text, you count).
88 its-the-text-we-want VALUE ' the quick brown fox '.
10 our-numeric-value PIC XX.
10 FILLER PIC X(what is left of the input line).
01 WS-FIRST PIC XX.
01 WS-SECOND PIC XX.
01 FILLER PIC X VALUE "N".
88 first-not-found VALUE "N".
88 first-found VALUE "Y".
MOVE your-input TO field-we-are-about-to-change
IF its-the-text-we-want
MOVE replacement-text TO the-bit-you-want-to-change
IF first-not-found
SET first-found TO TRUE
MOVE our-numeric-value TO WS-FIRST
ELSE
MOVE our-numeric-value TO WS-SECOND
END-IF
END-IF
如果输入是固定的,只需使用定义将其视为固定。可能有很多变化。
答案 1 :(得分:0)
你的问题听起来很适合有限状态机或简单的解析器。这听起来像是家庭作业,所以我不会为你编写代码,但我会提供一些可能指向正确方向的提示。
因此主要外观可能如下:
Perform Read-An-Input-Line
Perform until no-more-input
Perform varying II from 1 by 1
until II > length of Input-Line
Evaluate true
when 'the quick brown fox' = function lower-case( Input-Line (II:) )
...do replace for that string...
when 'fox' = function lower-case( Input-Line (II:) )
Move Input-Line (II + 5 : 2) to WS-Got-A-Number
End-Evaluate
End-Perform
Perform Read-An-Input-Line
End-Perform
我希望其中一些有帮助。