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
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
Free Guide: Managing storage for virtual environments
Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Don’t miss out on this exclusive content!
Discuss This Question: 3  Replies