|
|||||||
|
|
![]() |
|
|
|
|
||
|
Resolving Field Names at Runtime, Part 2 Dear Readers: In the May 9 issue of Midrange Guru, I provided one way that Jim could use one routine to process repeating fields from 15 files ["Resolving Field Names at Runtime"]. As I predicted, some of you have shared other ideas. It's obvious that some of you have wrestled with a problem like Jim's. Here are three proposed solutions. I have not tried to run any of these solutions, so I can't vouch for them. As with any code published here or elsewhere, use it at your own risk. --Ted Hey, Ted: I also encounter multiple-occurrence fields more than I had realized. I approach the challenge the way you do, but instead of listing all the field names (aaa01 thru aaa19), I use a basing pointer for the data structure. Here's an example. Enjoy! * this will map fields HA701-HA713 from file E3SHSTA * into array WeekSummPOS(1)...WeekSummPOS(13) d dsHSTA E DS ExtName(E3SHSTA) d @HA701 S * Inz(%Addr(HA701)) d DS Based(@HA701) d WeekSummPOS Like(HA701) Dim(13) --Efran Thanks for the idea, Efran. Your solution assumes that the fields are contiguous in the physical file. I think that's probably a safe assumption. The first solution I sent to Jim also used a pointer. Then I figured out a way to do away with the pointer. --Ted Hey, Ted: Did did you know you could use an externally described data structure and still append other fields to it? Your code can be simplified. Here's your code. D aaads ds D aaa01 D aaa02 D aaa03 D aaa04 D aaa05 D aaa06 D aaa07 D aaa08 D aaa09 D aaa10 D aaa11 D aaa12 D aaa13 D aaa14 D aaa15 D aaa16 D aaa17 D aaa18 D aaa19 D aaa overlay(aaads) D like(aaa01) D dim(19) Here is my code. D aaads e ds extname(fileaaa) D aaa overlay(aaads) D like(aaa01) D dim(19) --Jan Thanks, Jan. Like Efran, you assume that the fields are contiguous, which is okay. However, you also assume there is nothing else in the physical file, which is incorrect. --Ted Hey, Ted: One can use SQL to solve Jim's problem. Here is a very old bit of code that might demonstrate one way to go about it: H Debug c* Example of indirect reference to fields c* CRTSQLRPGI *CURLIB/FLDNAMES Dbgview(*Source) Objtype(*Module) c* CRTPGM PGM(FLDNAMES) ACTGRP(QILE) DETAIL(*BASIC) d TRUE s 10i 0 inz(0) d FALSE s like(TRUE) inz(-1) d DBField s 10a d DBFieldState s like(TRUE) d FldEOF s like(TRUE) d FldMsg s 50 d Sds d JobName 244 253 d CheckMand pr like(DBFieldState) d FldName like(DBField) const c* Spin through the database file looking for field names c/exec sql c+ declare Field cursor for c+ Select FLDNAME from FLDNAMES c+ order by FLDNAME c/end-exec c/exec sql c+ Open Field c/end-exec C Eval FldEOF = FALSE C DoW FldEOF = FALSE c/exec sql c+ Fetch next from Field into :DBField c/end-exec C If SQLCOD <> 0 C Eval FldEOF = TRUE C Else C Eval DBFieldState = CheckMand(DBField) C Eval FldMsg = 'Field ' + %trim(DBField) + C ' = ' + %editc(DBFieldState:'X') C FldMsg Dsply JobName C EndIF C EndDo c/exec sql c+ Close Field c/end-exec C Eval *InLR = *On * Check to see that the passed in field name contains * data. Since this is a boilerplate, error handling is minimal. p CheckMand b d CheckMand pi like(DBFieldState) d FldName like(DBField) const d FieldState s like(TRUE) d SqlStm s 512a d FldData s 512a c Eval FieldState = FALSE * We'll prepare a dynamic SQL statement to see if the field * contains data or not c/exec sql c+ Declare FldTest cursor for DynFldTest c/end-exec c eval SqlStm = 'Select ' + %trim(FldName) + c ' from Master' c/exec sql c+ Prepare DynFldTest from :SqlStm c/end-exec c/exec sql c+ Open FldTest using :SqlStm c/end-exec c/exec sql c+ Fetch next from FldTest into :FldData c/end-exec C If SQLCOD = 0 C if FldData <> *Blanks C Eval FieldState = TRUE C endIf C EndIF c/exec sql c+ Close FldTest c/end-exec c Return FieldState p e This indirect reference requirement is becoming more prevalent as people start using XML to move data around. Nice article, as usual! --Buck Thanks to all of you for sharing these ideas with other readers of Midrange Guru. --Ted
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |