MIDRANGE dot COM Mailing List Archive



Home » MIDRANGE-L » May 1999

Dynamic arrays



fixed



If you decide to try the dynamic array approach, you need something like this -
I've used a program described file for simplicity:  If lookup speed is a major
consideration take a look at the binary chop search technique an example of
which was recently published in News/400 (the code is on the web).
Alternatively prototype and use the C routine bsearch.



     FInpFile   IF   F   10        Disk


      * 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(InpData)

     D ArrayMax        S              3  0 Inz(50)
     D Count           S              5  0 Inz
     D Memory          S              7  0 Inz

     IInpFile   AA
                                                                                
  

     I                                  1   10  InpData


      * Allocate initial storage
     C                   Eval      Memory = %Size(Array) * ArrayMax
     C                   Alloc     Memory        Ptr

      * Loop reading file and storing records in array
     C                   DoU       %EOF(InpFile)
     C                   Read      InpFile

     C                   If        Not %EOF(InpFile)
     C                   Eval      Count = Count + 1

     C                   If        Count > ArrayMax

      * Need more space - increase by 20 & reallocate

     C                   Eval      ArrayMax = ArrayMax + 20
     C                   Eval      Memory = %Size(Array) * ArrayMax
     C                   REALLOC   Memory        Ptr
     C                   EndIf

     C                   Eval      Array(Count) = InpData

     C                   EndIf
     C                   EndDo

      * Do whatever other processing is required

      * Don't forget to free up array when no longer needed

     C                   DEALLOC                 Ptr

     C                   Eval      *InLR = *On


+---
| This is the Midrange System Mailing List!
| To submit a new message, send your mail to MIDRANGE-L@midrange.com.
| To subscribe to this list send email to MIDRANGE-L-SUB@midrange.com.
| To unsubscribe from this list send email to MIDRANGE-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---






Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2014 by MIDRANGE dot 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 here. If you have questions about this, please contact