Page 1 of 1

Whats the problem with the code..

PostPosted: Wed Apr 18, 2012 4:54 pm
by Nara_513
I've written a Easytrieve program which reads two input files and compares the keys present in both the files and, if matched, an arithmetic operation should take place, if not then the first file record should be written to the output file.

Below is the code:


  FILE POSFILE
       POSFILE-REC       01    39   A
       IN-POS-ID    01 11 N
       IN-POS-AMT   30 05 N
  FILE NEGFILE
      NEGFILE-REC       01    39   A
      IN-NEG-ID    01 11 N
      IN-NEG-AMT   30 05 N
 FILE OUTFILE1
      OUT1-REC          1   80  A
      IN-OUT-ID    01 11 N
      IN-OUT-AMT   30 05 N
 WS-POS-ACCT-ID      W   11  N
 WS-POS-ACC-AMT      W   05  N
 WS-NEG-ACCT-ID      W   11  N
 WS-NEG-ACC-AMT      W   05  N

 **************************************************************
JOB INPUT POSFILE
   WS-POS-ACCT-ID = IN-POS-ID
   MOVE IN-POS-ID TO WS-POS-ACCT-ID
   DISPLAY WS-POS-ACCT-ID
   WS-POS-ACC-AMT = IN-POS-AMT
    GET NEGFILE
         DO WHILE NOT EOF NEGFILE
            WS-NEG-ACCT-ID = IN-NEG-ID
             WS-NEG-ACC-AMT = IN-NEG-AMT
              DISPLAY WS-POS-ACC-AMT ' WS-POS-ACC-AMT'
              DISPLAY 'IN WHILE LOOP'
              DISPLAY WS-NEG-ACC-AMT 'WS-NEG-ACC-AMT'
        IF WS-NEG-ACCT-ID EQ WS-POS-ACCT-ID
            DISPLAY 'IN IF LOOP'
            IN-OUT-ID = WS-POS-ACCT-ID
            WS-POS-ACC-AMT = WS-POS-ACC-AMT - WS-NEG-ACC-AMT
       ELSE
          DISPLAY 'IN ELSE LOOP'
          IN-OUT-ID = WS-POS-ACCT-ID
          IN-OUT-AMT = WS-POS-ACC-AMT
          DISPLAY IN-OUT-AMT 'IN-OUT-AMT'
      END-IF
    GET NEGFILE
  END-DO
  PUT OUTFILE1
  GOTO JOB
 STOP



My program is able to check the first record of the first file ... but when it tried to process the second one it ended wth RC=16.

Re: Whats the problem with the code..

PostPosted: Wed Apr 18, 2012 5:25 pm
by BillyBoyo
Well, for the first input record on your first file, you read the entire second file. Not going to work.

I've no idea what your RC=16 means. Don't you get any messages?

Easytrieve has "synchronised file processsing" which is one way to do what you are after.

For any reasonable matching, your files have to be in the same sequence as the match. Are they?

If you don't want to use the synchronised processing, you have to code it yourself which means stopping reading file2 when it has a higher key than file 1, and not reading it again until you know you don't need the current record on it.

Re: Whats the problem with the code..

PostPosted: Wed Apr 18, 2012 10:13 pm
by Nara_513
Actually the job is abending after comparing the first record with all the records in the second file, then it is terminating when it tried to compare the second record of the first file with that of second file...


The file keys are in sequence, but both the files doesnt have all the keys ...i mean keys present in first file may not present in the second one...also their are records which have same key...

I have a doubt here, since i have given the condition " DO WHILE NOT EOF POSFILE" does it mean that we can execute the loop only once ...? as once the file reaches EOF...it cannot browse the records from the start again....

also can you please explain..: The first statement: "....Not going to work.."


Thanks..

Re: Whats the problem with the code..

PostPosted: Wed Apr 18, 2012 10:36 pm
by BillyBoyo
You read all the records on file 2 for your first record on file 1. You have no way of re-opening file 2 (which would be a very bad solution anyway) so it is not going to work.

Review the rest of what I said, please.

Re: Whats the problem with the code..

PostPosted: Wed Apr 18, 2012 11:40 pm
by BillyBoyo
OK, here are your two files. I have "normalised" them to a one-byte key, with one-byte of data (the key).

File 1
1
2
4
5
6
9

File 2
1
2
2
3
5
5
6
8
9


Please explain what you would like to do for each record on File 1, and what you want to do with the unmatched records from File 2.

Also, can there be duplicate keys on File 1?

Re: Whats the problem with the code..

PostPosted: Thu Apr 19, 2012 8:39 am
by Nara_513
Hi Bill,

I need to compare each record of file 1 with that of file 2, if dont have a matched ones in file 2 , the record of teh first file will be written to file 1, else we will do an arithemtic operation with the corresponding values for the key in both the files.

We wil be just writing the first file records only..as all the records in file2 have a match in file 1.

Their can be duplicate keys inboth the files...

Thanks

Re: Whats the problem with the code..

PostPosted: Thu Apr 19, 2012 11:19 am
by BillyBoyo
OK, so...

File 1
1 1000
1 0040
1 0020

File 2
1 020
1 030
1 040
1 060


How would you calculate if those are the values, say we just want to do an ADD, but with your matching logic. What would be the results?

Re: Whats the problem with the code..

PostPosted: Thu Apr 19, 2012 11:36 am
by BillyBoyo
Presumably you can't have the 4th File 2 record in the above exampke?

If you want the 1:1 match here, 1000+020, 0040+030, 0020+0040, then you may not be able to use the synchronised file processing, unless it has changed since I last used it (which is certainly possible).

If you find the synchrnoised processing in the manual, code up a little job to match the above files (the two incorporated) and DISPLAY your results for MATCHED, FILE1 and FILE2 you'll discover exactly whether you can use the synchorised processing. What used to happen is that 1 1000 would be "held" whilst 1 020, 1 030 and 1 040 were processed, then 1 0040 would be read, and there would be no match.

If it still works that way, you'll need to code the match out, but let us know first.