340 pts.
 Can I Declare a variable-length Array?
Is it possible to Declare a variable-length Array viz.. like we have in "VARLEN" in DDS?

Software/Hardware used:
RPGILE, RPG/400, AS/400
ASKED: March 16, 2012  1:31 PM
UPDATED: March 17, 2012  1:57 PM
  Help
 Approved Answer - Chosen by svankipu (Question Asker)

Sure in RPGLE but not in RPG/400 .
The length of each field can be varing, but not the number of elements.

D Temp60 S 60 Varying DIM(30)

Each element of Temp60 will take 62 bytes of memory.
If the data isn't coming from a varchar field you will have to %Trim whatever field it comes from. You may also have to deal with null value issues.
Phil

ANSWERED:  Mar 16, 2012  5:37 PM (GMT)  by svankipu

 
Other Answers:
Last Wiki Answer Submitted:  October 16, 2012  5:55 pm  by  Michael Tidmarsh   11,410 pts.
Latest Answer Wiki Contributors:  Michael Tidmarsh   11,410 pts.
To see other answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Can I Declare a variable-length Array?

Do you want the array to be varying? Or do you want the array elements to be varying?

If you want an array to hold more or fewer elements at different times, you’ll need to use ILE RPG. The array will be defined with DIM() that declares the maximum number of elements allowed. It will also be defined over memory that is allocated at run-time to cover the size that you need. Use the ALLOC, REALLOC and DEALLOC statements to manipulate the allocated memory. (There are also %alloc() and %realloc() builtin functions.)

When you allocate memory for a number of elements, it is your responsibility not to reference an element beyond the end of your memory, regardless of how many elements were declared in the DIM() keyword.

Alternatively, you don’t actually need a DIM(). After you allocate some memory, you can simply use pointer increments and decrements to set a new position for a variable or for a DS.

Tom

 108,360 pts.

 

So answer is yes, for RPGLE for two different ways of reading the question
Answer is no for RPG/400.

 44,220 pts.