Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 32 -- May 9, 2003

Resolving Field Names at Runtime


Hey, Ted:

I have 15 files that have 19 "bucket" fields. In one file, for example, they are named CAS01 through CAS19. I need to find the last field that has data in it. The easiest way is to test each field individually until I get a non-blank, but since I have 15 files that are similar I would like to write one routine, not 15 routines.

I envision code that would work something like this:

c        eval      fieldname = 'CAS'
c        eval      subscript = 19
 * loop through fields to find last one 
                        with characters in it.
c        dou       subscript & 1
c        eval      %subst(fieldname:4:2) = %char(subscript)
c        if        'Fieldname' = *blanks
c        eval      subscript = subscript - 1
c        iter
c        endif

What do I put for Fieldname in the compare?

--Jim


Some languages allow you to resolve variable names at run-time, but RPG is not one of them.

There are probably several ways you could write one routine that will work with all 15 files. I will give you one, and won't be surprised if some of your fellow readers of this newsletter send in other solutions.

In my example, there are only 2 files, not 15. They are FILEAAA and FILEBBB, and they have repeating groups AAA01 through AAA19 and BBB01 through BBB19 respectively. Here's the code:

     H dftactgrp(*no) actgrp(*new)
     H option(*srcstmt: *nodebugio)

     Ffileaaa   if   e           k disk
     Ffilebbb   if   e           k disk

     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)

     D bbbds           ds
     D   bbb01
     D   bbb02
     D   bbb03
     D   bbb04
     D   bbb05
     D   bbb06
     D   bbb07
     D   bbb08
     D   bbb09
     D   bbb10
     D   bbb11
     D   bbb12
     D   bbb13
     D   bbb14
     D   bbb15
     D   bbb16
     D   bbb17
     D   bbb18
     D   bbb19
     D   bbb                           overlay(bbbds)
     D                                 like(bbb01)
     D                                 dim(19)

     D LastUsed        pr            10i 0
     D   array                             like(aaa01)
     D                                     dim(19)

     D Pos             s             10i 0

     C                 read      recaaa
     C                 eval      Pos = LastUsed (aaa)
     C                 read      recbbb
     C                 eval      Pos = LastUsed (bbb)
     C                 eval      *inlr = *on
     C* ==================================================
     P LastUsed     b
     D              pi            10i 0
     D   list                              like(aaa01)
     D                                    dim(19)

     D ix             s             10i 0

     C              for       ix = 19 downto 1
     C              if        list (ix) <> *blanks
     C              return    ix
     C              endif
     C              endfor
     C              return    *zero
     P              e

I load the repeating fields into data structures AAA and BBB to be sure that the fields will be stored contiguously in memory. I want you to notice something worth remembering: I have not defined size information for the subfields. The compiler will figure out where each subfield begins and ends based on the field definitions in the files. I don't think RPG was always so accommodating, but I can't tell which release was the first to offer this feature. I compiled this example under V5R2.

Then I redefine the repeating groups as arrays, which I can pass to subprocedure LastUsed, the common routine that processes all of the files. The ability to pass parameters to subprocedures is a good reason why RPG programmers need to get used to using subprocedures and quit using subroutines exclusively.

This is probably not the only way to solve your problem, and it may not be the best way, but it beats cloning and slightly altering a routine 14 times in order to handle all those files.

--Ted


Sponsored By
WORKSRIGHT SOFTWARE

600 Billion

That's how much a recent independent study estimated U.S. businesses spend on dirty data. How much of that 600 billion is spent by your company? Cleanse you dirty ZIP Codes and mailing addresses with our software and save big bucks.

WorksRight Software, Inc.
Phone: 601-856-8337
E-mail: software@worksright.com
Web site: www.worksright.com


THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS

Sending AS/400 Spool Files

Resolving Field Names at Runtime

Reader Feedback and Insights: COBOL Examples Wanted


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Shannon O'Donnell

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.