Search code examples
arraysrpglerpg

Accept Array of Unknown Size in RPG Procedure


I am trying to make a procedure that will take an array and return a count of the used elements (why is this not a BIF??). I am struggling to find a way to pass an array of unknown size to my procedure.

Something like...

P count         B
D count         PI              3  0 
D  array                         *
D  size                        10  0
D  elems                        3  0
 *
D ct            S               3  0
 /free
  // find the first blank or zero element and return 
 /end-free
P count         E

Obviously I am new to this, so I have a couple of questions:

  1. Is there any way around passing the size and max elements as parameters (or maybe a way of passing a sort of header packet with details about the data)?
  2. Is there any way of determining what type the data is? (so I know whether to look for *ZEROS or *BLANKS)
  3. Am I missing some other, better approach to this problem?

I know I could keep a separate counter variable that gets incremented whenever I set an element in the array, but I would really like to find a better solution... thank you for reading.


Solution

  • I've been programming in RPG for... well, for a very long time. It's marvelous to see a new RPG programmer and it's doubly marvelous to see modern coding concepts used from the get-go.

    RPG started life with fixed-size everything. Fixed size strings, fixed size numbers and fixed-size arrays. Being a strongly typed language based on punched cards, this all made sense. Of course, modern computing requirements tend to want varying-sized strings, numbers and arrays. RPG is still strongly-typed, so a given variable can only ever be a string or a number; it can never switch types. Which also means that the compiler hasn't got a means to determine the variable type at run time - the variable type is immutable and the programmer and compiler both know what that type is. Oh well.

    As for your varying length array, there's some hope for that. Mihael Schmidt has a service program called ArrayList which might handle many of your needs.