× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Re: Multi Dimensional Array
  • From: dhandy@xxxxxxxxxxx (Douglas Handy)
  • Date: Wed, 11 Aug 1999 18:33:38 GMT

Rajeev,

>Is there any way to process multi-dimensional array in RPG?
>For example, there is a complile time array with 100 records and each record
>with 60 chars.
>If I want to change 35th- 50th positions of the 10th records, how can I do it?

That isn't what I normally think of as a multi-dimensional array,
unless the 60 chars in each record are supposed to be discrete array
elements.  Saying you want to change positions 35-50 of the 10th
record implies to me that perhaps you truly should have a single
dimension array of 100 elements of 60 bytes each, and you just want to
change a subset of an element.

As other responses point out, using %subst() or overlay are both very
effective ways of doing that.  Much better than the RPG III technique
of using 1-byte array elements to overlay something else just for the
sake of substringing.

But in the event you need a multi-dimensional array, my experience is
that you typically only want to reference one element at a time to
either save or retrieve the element's contents.  With RPG IV, my
preference is to  just code a little subprocedure to return the index
when passed the desired dimensions.

 c                   eval      Array( GetArrayIndex( X: Y )) = A

or 

 c                   eval      A = Array( GetArrayIndex( X: Y ))

This technique allows you to code as many dimensions as you want, and
still be (relatively) readable.  I don't put this one in a service
program, because I change the name of the procedure based on the
array, and change the number of arguments and array sizes.

By making the arguments Const, you can pass a variable, expression, or
constant for each dimension.  For example, a simple program:

     D Entry           S             10A
     D Array           S                   Like( Entry )
     D                                     Dim( 6000 )


     D GetArrayIndex   PR             5I 0
     D  X                             5I 0 Const
     D  Y                             5I 0 Const

     C                   Eval      Array( GetArrayIndex( 1: 2 )) =
'Rhinoceros'
     C                   Eval      Entry = Array( GetArrayIndex( 1: 2
))
     C                   Eval      *InLR = *On

     P GetArrayIndex   B

     D GetArrayIndex   PI             5I 0
     D  X                             5I 0 Const
     D  Y                             5I 0 Const

     D Array_Size_X    C                   100
     D Array_Size_Y    C                   60

     C                   Return    (( X - 1 ) * Array_Size_X + Y )

     P                 E
+---
| 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
+---END



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 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.