Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CHAIN & READE 1

Status
Not open for further replies.

rstitzel

MIS
Apr 24, 2002
286
US
I have an Invoice Detail File. The file contains Route&Seq, Cust ID, Block Number, Invoice Number, Product Code, Product Quantity etc.

The keys are ROUTESEQ, CUSTID, BLOCK NUM & INVOICE NUMBER.

I've created a KLIST named FILEKEY.

What I want to do is chain out to a specific invoice in this file and read the product codes.

Will this work:

EVAL X =1
FILEKEY CHAIN WDOINVD
EVAL PRODCODE(X) =OIPRDCD
DOW NOT %EOF
FILEKEY READE WDOINVD
EVAL PRODCODE(X) =OIPRDCD
X ADD 1 X
ENDDO

My thinking is the CHAIN will find the first matched record and the READE will read each matching sequential record. Not sure if that's correct.

Please advise. Thank you
 
I don't use the CHAIN cmd when I need to read multiple records from a file. This is how I do it, (free form):
Code:
/Free

 X = 1;
 SetLl FileKey WdoInvd; 
 If %Equal(WdoInvd);
   ReadE FileKey WdoInvd;
   DoW Not %EoF(WdoInvd);
     Prodcode(X) = OiPrdCd
     X = X + 1;
     ReadE FileKey WdoInvd;
   EndDo;
 EndIf;

/End-Free
The SETLL cmd sets the file pointer to the first record that matches the key list FILEKEY. Then the READE command will read each record (starting with the first one) that is equal to FILEKEY. The problem with using the CHAIN opcode, is that it will not turn on %EOF if it doesn't find a record equal to FILEKEY, using the SETLL, if the
%EQUAL if on (record that matches FILEKEY found), then you can READE and check for %EOF. Hope this makes sense.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Yes,

You're correct that a chain/reade combo will work that way. You should put IF %FOUND after the CHAIN opcode, though.

Regards,

Tom.
 
But, if you issue a READE after the CHAIN, then you're going to miss the first record, since the CHAIN does both the SETLL and READE combined. You would have to code the CHAIN, check for the %Found condition, then do your processing, then READE. It would look something like this:
Code:
X = 1;
 Chain FileKey WdoInvd; 
 If %Found(WdoInvd);
   ProdCode(X) = OiPrdCd;
   X = X + 1;
   ReadE FileKey WdoInvd;
   DoW Not %EoF(WdoInvd);
     Prodcode(X) = OiPrdCd
     X = X + 1;
     ReadE FileKey WdoInvd;
   EndDo;
 EndIf;

I like using SETLL and READE because there is less coding to do.

RedMage1967
IBM Certifed - RPG IV Progammer
 
I like less code :)

Thanks to you both for your input.
 
Oops, I didn't see that the READE was at the top of the loop. If you're gonna use that method, it has to be on the bottom of the loop.

Less code can be a good thing and perhaps more readable.

Tom.
 
tcsbiz

How are you going to loop it? CHAIN does not set %EOF on if a record is not found.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Use an until loop.

In the olden days I did it this way:

KEY CHAIN FILE 90
*IN90 DOWEQ *OFF
.
.
KEY READE FILE 90
ENDDO

The point is that the technique works and the READE has to be on the bottom.

Tom.
 
tcsbiz

In free form, you don't use indicators any more. You use the BIF's %Found (for CHAIN) %Equal (for SETLL) and %EOF (for all READ's). Using the indicators on these opcodes is not allowed in free form.

RedMage1967
IBM Certifed - RPG IV Progammer
 
I will use CHAIN when the file (physical or logical) has a UNIQUE key, and I am chaining using all the keys. In our system, most of our files have unique keys. For partial keys, I will use SETLL/READE, conditioning the entire READE loop on whether the SETLL %EQUAL BIF finds something.


"When once you have tasted flight, you will forever walk the Earth with your eyes turned skyward, for here you have been, and there you will always long to return."

--Leonardo da Vinci

 
Red,

The original post did not state free-format. It was asking about fixed-format. At least that what I took from the code example.

Also, I stated that I did that in the OLD days, BFF (before free-format).

But the point is, the code works as I described it. I didn't describe it in free-format.

But, you're right. The technique is not allowed in free-format but that is not relevant here. If the original post had asked about free-format, I would have posted different code.

Tom.
 
tcsbiz

Meant no insult. If fact, when I wrote my orignal code in fixed, I did it exactly the same way you did. But, since I've been doing free form, I have a tendency to think in free form, not fixed. Also, the BIF's work in RPG IV (fixed format).

RedMage1967
IBM Certifed - RPG IV Progammer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top