Find instance same text on different records, with different actions as a result

92 Views Asked by At

so my input file would be something like:

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

now, I'd like to be able to grab the 1st instance of "fox" and capture the "23" in WS-FIRST then grab the second instance of "fox" and capture "14" in WS-SECOND.

I'll be replacing "the quick brown fox" with a different string as well, but it's the same on both lines, so pretty easy.

The text is fixed in content and fixed in position and the number is also fixed in content, position and length.

2

There are 2 best solutions below

8
On BEST ANSWER

Your problem sounds like it would be well suited by a finite state machine or a simple parser. This sounds like homework, so I won't write the code for you, but I'll offer some hints that might point you in the right direction.

  • Process your input in a char by char loop, resist the urge to scan for words.
  • Compare your input at that char to the longest literal first, so "quick brown fox" will hit before "fox". This is called "greedy" scanning, and it is usually, but not always, the right way to go.
  • Unless your search is case insensitive, you must case fold the input with function upper/lower-case
  • When comparing to literals, place them first in the condion, this can make it easier to get a compiler error instead of a subtle bug in some cases and languages

So a main look might look like:

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

I hope some of that helps.

2
On
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

If the input is fixed, just use definitions to treat it as fixed. Lots of variations possible.