Page 1 of 1

Cobol query - problems reading correct line from input file

PostPosted: Wed May 11, 2011 12:37 am
by dbhasty
Hi

I was wondering if anyone would be able to help me.

I have the following input file

01JOHN
02STEPHEN
03JIM
04ROBERT
05RYAN
06MARK
07GLEN
08WILLIAM
09BEN
10HARRY
11ALEX
12BRIAN


It basically gives a month of the year and a person for that month.

So i have some code that works out previous month and it seems to be working ok. But what i want to do next it get the persons name beside that month.
So for april - month 04, i want to get the name robert.

I have this code so far:

READ INFILE.

PERFORM UNTIL (INPUT-MONTH = WS-PREVIOUS-MONTH) OR EOF
   READ INFILE77
       AT END SET EOF TO TRUE
   END-READ
END-PERFORM.

MOVE INPUT-NAME TO WS-SELECT-NAME.


When i display WS-PREVIOUS-MONTH is correctly displays the the previous month - 4.
However when i display the name is incorrectly displays the last name on the list.

Can any one see what is wrong with my code or suggest a way to fix this.

Many thanks.
DB

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 1:09 am
by NicC
It would appear that nowhere do you put a value into WS-PREVIOUS-MONTH therefore your perform carries on until EOF and the name will be BRIAN

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 1:26 am
by dbhasty
WS-PREVIOUS-MONTH has been worked out in code before that section i put above and it gets displayed ok.

ACCEPT YYMMDD             FROM DATE YYYYMMDD.
MOVE   YYMMDD-YY         TO   CURRENT-DATE-YYYY.
MOVE   YYMMDD-MM        TO   CURRENT-DATE-MM.
MOVE   YYMMDD-DD         TO   CURRENT-DATE-DD.

EXEC SQL
   SET :CURRENT-DATE-1M  =   CURRENT DATE - 1 MONTH
END-EXEC.

MOVE CURRENT-DATE-1M  TO    SELECT-DATE.
MOVE SELECT-DATE-YYYY  TO    WS-SELECT-YEAR.
MOVE SELECT-DATE-MM     TO    WS-PREVIOUS-MONTH.


When i display WS-PREVIOUS-MONTH is correctly displays 4.

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 1:37 am
by BillyBoyo
Is INPUT-MONTH defined correctly? If not, you will get to EOF (as seen from BRIAN) but since you are not looking at the month from the input file but the month you have calculated, it seems likely your test is still not working, and the month from your search of the file is 12.

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 1:52 am
by dbhasty
Here is the input file where INPUT-MONTH is contained.

FD  INFILE77
    RECORDING MODE F
    RECORD CONTAINS 80 CHARACTERS
    BLOCK CONTAINS 0 CHARACTERS
    LABEL RECORDS ARE STANDARD.
01  INFILE-RECORD.
    03 INPUT-MONTH       PIC 9(02).
    03 INPUT-NANE       PIC X(10).


Just noticed INPUT-MONTH is a PIC 9 and WS-PREVIOUS-MONTH is a PIC X. Would this have anything to do with it?

I cant change WS-PREVIOUS-MONTH, its causing a error because it came from a PIC X that was used in the DB2 SQL code, i cant seem to use PIC 9 in that.

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 2:09 am
by dbhasty
Ive changed INPUT-MONTH to a PIC X, however im still just getting the last file name on the list. Is there a different way to compare two PIC X's?

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 4:53 am
by Robert Sample
When i display WS-PREVIOUS-MONTH is correctly displays 4.
This is wrong. It should display as 04 to match INPUT-MONTH. If it does not display as 04, the match will fail and the last name in the input file would be selected since there would not be a match.

You are needlessly complicating your code for some reason. Create a table of 12 name variables. Open the input file, use it to load the table, then close the file. From that point on, use the table to find the names. The logic is not hard:
IF  CURRENT-MONTH = 01
    MOVE 12 TO PREVIOUS-MONTH
ELSE
    COMPUTE PREVIOUS-MONTH = CURRENT-MONTH - 1
END-IF.
MOVE TABLE-NAME (PREVIOUS-MONTH) TO ...
Doing this in SQL is not necessary.

Re: Cobol query - problems reading correct line from input f

PostPosted: Wed May 11, 2011 5:56 am
by BillyBoyo
Robert's method is much simpler than yours. Where you go Jan-Dec, amend the year as well if you are using that.

In your method, EOF is actually not valid (the month you are looking for should always be in the file). So, handle it. You are doing a "priming" read, if you've managed to get a typo on that (INFILE instead of INFILE77), you should check there for EOF as well. Also an invalid condition, but these things happen, so deal with them correctly. What about other file error conditions? Also deal with them.

As to why it is not working, again Robert is right. If your display is "4" not "04" then you will not be getting a match with PIC XX to PIC 99 (or with PIC XX to PIC XX).

When displaying data I always put ">" "<" around the data, so you can also tell the number of characters/numbers the field has. You could then see if you had a leading or trailing blank, or if your field is only one character long.