Cobol query - problems reading correct line from input file



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Cobol query - problems reading correct line from input file

Postby dbhasty » Wed May 11, 2011 12:37 am

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
dbhasty
 
Posts: 6
Joined: Fri May 06, 2011 11:05 pm
Has thanked: 0 time
Been thanked: 0 time

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

 

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

Postby NicC » Wed May 11, 2011 1:09 am

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
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 2690
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisys (almost)
Has thanked: 4 times
Been thanked: 105 times

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

Postby dbhasty » Wed May 11, 2011 1:26 am

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.
dbhasty
 
Posts: 6
Joined: Fri May 06, 2011 11:05 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby BillyBoyo » Wed May 11, 2011 1:37 am

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

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

Postby dbhasty » Wed May 11, 2011 1:52 am

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.
dbhasty
 
Posts: 6
Joined: Fri May 06, 2011 11:05 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby dbhasty » Wed May 11, 2011 2:09 am

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?
dbhasty
 
Posts: 6
Joined: Fri May 06, 2011 11:05 pm
Has thanked: 0 time
Been thanked: 0 time

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

Postby Robert Sample » Wed May 11, 2011 4:53 am

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.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 222 times

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

Postby BillyBoyo » Wed May 11, 2011 5:56 am

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post