` Printed Icetips Article

Icetips Article



Par2: Declaring an array at runtime
2000-02-14 -- John Welsh
 
I wanted to share this code with everyone. I worked out a way to declare
multi-dimensional arrays art runtime. I believe they could be REDIM'd in a
round about way. The code below works. Just create a .prj and turn of range
checking. You will see I took several people's ideas and put them together
with my own. Thanks to all who helped.

The idea here is to set up a 1 dimensional dynamic array  that can be
accessed as if it is a multi-dimensinal array by calculating a pointer into
the one DIM array. The array could be REDIM'd by creating a NEW instance of
the curent one, copying the contents, DISPOSE of the first array, create it
again using new DIM's and then copy the saved array back into the just
created one. Finally, DISPOSE of the saved copy of the array. Now you will
have a new array of a different size. The code for the copy back might be a
bit tricky, but doable.

!******************************************************
tstarray   program

   map
     testa
   end


Test CLASS,TYPE
a SREAL,DIM(2)             !Will let compiler know this is an array
  END

x LONG     !(x(y) + 1) Size(SREAL) --> will determine size of array
y LONG     !

x1  LONG   !Subscript values
y1  LONG   !


            code
            testa

testa    procedure

t & Test
s & STRING
xy  LONG
ViewIt   & CSTRING

         code

!for t.a,DIM(x,y)  where x = 10, and y = 5
!    (normal)                                       (dynamic)
!    t.a[x1,y1] is equivalent to --> t.a[(x1 - 1) * y + y1] This is how the
subscripts translate from
!
a normal Clarion array to a dynamic one

  x = 10
  y = 5
  xy = (x * y) * 4 + 4   !4 bytes for an SREAL + 4 to account for not using
the 0 position

  ViewIt &= NEW(CSTRING(xy + 100))

  ViewIt = '2 DIM array results|'

  s &= NEW(STRING(xy))
  t &= ADDRESS(s)
  LOOP x1 = 1 TO x
    LOOP y1 = 1 TO y
      t.a[(x1 - 1) * y + y1] = y1 + (100 * x1)
      ViewIt = ViewIt & FORMAT(t.a[(x1 - 1) * y + y1],@n_5)
    END ! loop
    ViewIt = ViewIt & '|'
  END
  ViewIt = ViewIt & 'End'

  Message(ViewIt)

  DISPOSE(t)
  DISPOSE(ViewIt)



Printed May 6, 2024, 7:58 am
This article has been viewed/printed 35132 times.