|
Jon Paris posted some code that shows how to use RPG IV to make a
dynamic-sized array. Modified for my own testing, here it is (any errors
are mine, not Jon's):
PDYNARRAY b
DDYNARRAY pi
* Note: the value supplied to the DIM statement is used by RPG as
* the maximum size that the array can grow to - since the array
* is being dynamically sized this is used as a "sanity check"
* only. i.e. If it ever gets this big the program is looping !!
D Array S Dim(20000) Based(Ptr) Like(NAME)
D CurrElems S 3 0 Inz(50)
D RecordsRead S 5 0 Inz
D Memory S 7 0 Inz
* Allocate initial storage
C Eval Memory = %Size(Array) * CurrElems
C Alloc Memory Ptr
* Loop reading file and storing records in array
C DoU %EOF(Master)
C Read Master
C If Not %EOF(Master)
C Eval RecordsRead = RecordsRead + 1
* Limit this example to 200 records
C If RecordsRead > 200
C Leave
C EndIf
* Need more space - increase by 20 & reallocate
C If RecordsRead > CurrElems
C Eval CurrElems = CurrElems + 20
C Eval Memory = %Size(Array) * CurrElems
C REALLOC Memory Ptr
C EndIf
C Eval Array(RecordsRead) = NAME
C EndIf
C EndDo
* Don't forget to free up array when no longer needed
C DEALLOC Ptr
PDYNARRAY e
If you search the archives at http://www.midrange.com I'm sure you'll see
earlier threads on dynamic arrays, as well as performance considerations on
the use of arrays as a cache for disk files (David Morris has some
interesting posts).
Try it with a disk file first and measure the performance. Then try it with
a user space and measure the performance. Then try it with dynamic arrays.
I would put the routine in a separate procedure so that you can easily swap
the implementation details.without affecting the mainline code, e.g.:
eval ReturnCode=AddEntry(NewValue)
eval ReturnCode=RmvEntry(OldValue)
With disk files, AddEntry would do a Chain/Write. With arrays it would bump
the highest element number and store NewValue at ARR(NewElement). You get
the idea.
Buck Calabro
> -----Original Message-----
> From: Dass, Naveen
> Sent: Monday, November 01, 1999 10:02 AM
> To: 'RPG400-L@midrange.com'
> Subject: RE: SUBFILE FOR NON DISPLAY PROGRAMMING....
>
> Hi all,
>
> If We use an array or multiple occurence data structure, We have to
> define the exact size and number of entries up front. By using a subfile I
> think we can define a certain number of entries, but have the entries
> extend
> themseleves Automatically if more are needed. I think this should keep the
> program size small, while allowing the flexibility to accomodate a growing
> number of entries if needed.
>
> Naveen Dass
>
>
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.