Agent Mahone
Agent Mahone

Reputation: 307

How to check for '3 words' in a input cobol variable of length 10, if found skip record?

INPUT: WS-Variable contains '345xABCx12'

Code: IF WS-Variable string contains 'ABC' Display ' SKIP!!!' Else Perform something.

If variable contains 'abc'
  display skipped
else
  process-para
end if.

Upvotes: 1

Views: 3115

Answers (2)

Bill Woodger
Bill Woodger

Reputation: 13076

Did you try something like:?

If variable equal to 'abc'
  display "skipped"
else
  perform process-para
end-if

This would assume variable is defined as PIC XXX or X(3).

If that does not suit, please update your question with a fuller description, some sample input, expected output and what you have tried.

Now it turns out you are looking for 'abc' at a variable location within a piece of data.

There are several ways to do it.

Easiest is

INSPECT field-you-want-to-look-at 
  TALLYING a-count 
  FOR ALL value-you-want-to-search-for

a-count can be defined as BINARY PIC 9(4). value-you-want-to-search-for as PIC XXX VALUE 'abc'.

MOVE ZERO TO a-count before the INSPECT.

After the INSPECT you can test a-count which will tell you how many occurences of value-you-want-to-search-for there are in field-you-want-to-look-at.

The reason to use a data-definition (PIC XXX) instead of a literal ('abc') is for ease of maintenance and understanding. There may be more than one place where 'abc' is required in the program, and it may mean the same thing in both places, or one thing in one place and something else in another. With a data-name from the definition you can describe what 'abc' means in each instance. If the value of 'abc' (or one of the 'abc's) needs to change, there is only one place it needs to be changed - in the working-storage.

If (and assuming Enterprise COBOL on the Mainframe due to the COOLGEN reference) you use compiler option OPT(STD) or OPT(FULL) a data-name which is referenced but is never the "target" of anything, ie it has a constant value, is treated as a constant. So you get a named constant as well.

INSPECT FLIGHTPLAN-REFERENCE 
  TALLYING NO-OF-ENTRIES-TO-EU-AIRSPACE
  FOR ALL EU-FLIGHTPLAN-CODE

Is much more easy to understand than

INSPECT VAR1 TALLYING A-COUNT FOR ALL 'abc'

Upvotes: 1

NealB
NealB

Reputation: 16928

You are looking for the INSPECT verb... Try something like...

   IDENTIFICATION DIVISION.
   PROGRAM-ID. EXAMPLE.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01  TESTDATA  PIC X(50).
   01  COUNTER   PIC 9(4).
   PROCEDURE DIVISION.
       MOVE '12345XXABCXX12345' TO TESTDATA
       MOVE ZERO TO COUNTER
       INSPECT TESTDATA TALLYING COUNTER FOR ALL 'ABC'
       IF COUNTER > 0
          DISPLAY 'SKIP! ' TESTDATA
       ELSE
          DISPLAY 'DONT SKIP ' TESTDATA
       END-IF
       MOVE '12345XXZZZXX12345' TO TESTDATA
       MOVE ZERO TO COUNTER
       INSPECT TESTDATA TALLYING COUNTER FOR ALL 'ABC'
       IF COUNTER > 0
          DISPLAY 'SKIP! ' TESTDATA
       ELSE
          DISPLAY 'DONT SKIP ' TESTDATA
       END-IF
       GOBACK
       .

Upvotes: 2

Related Questions