[Date Prev][Date Next][Thread Prev][Thread Next]
[Author Index] [Date Index] [Thread Index]
[SQR-USERS Info] [SQRUG Home Page]

Re: Dynamic Array Names



>
> SQR-USERS,
>
> Can the array names be dynamic?
> I have a generic array input routine which would work much more
> smoothly if I could get it to work with a changeable ARRAY NAME in the
> GET and PUT commands.
> I have had to use an 'if' command to distinguish which array to
> update.
>
>
> if $array-name = 'dparray'
>    put $something into dparray(#record)
> end-if
> if $array-name = 'wcarray'
>    put $something into wcarray(#record)
> end-if
>
>
> I am new to SQR.  I have taken the beginners and advanced courses, but
> still feel I have allot of learning to do.
>
> Angel GUZMAN

Angel,

Array names cannot be dynamic, nor can they be passed as
parameters to procedures.  If you're willing to create a
ufunc (written in C and linked in with SQR), I think you
can create a reusable routine that allows you to pass
the name of the array, but then you'd be writing in C.

I don't know if this will work for your application, but
you may be able to use substitution variables
to allow you to reuse the code using #include files
and require that the name of the array be #defined.
For example:

#define MY_ARRAY wcarray
#include 'my_proc.sqh'

begin-procedure main
create-array name=wcarray size=100 field=a:char
do my_proc( #n )
end-procedure

where my_proc.sqh looks like:

begin-procedure my_proc( :#n )
move 0 to #n
input $line
while not isnull( $line )
   put $line into {MY_ARRAY}(#n) a
   add 1 to #n
end-while
end-procedure

Unfortunately, this doesn't quite allow you to pass
the array as a parameter, but does allow you to
declare the array differently in different programs
that use the routine.  If you need to use the same
routine several times in your program using different
arrays but want to have a single source-code module,
try this:

#define MY_ARRAY darray
#include 'my_proc.sqh'
#define MY_ARRAY wcarray
#include 'my_proc.sqh'

begin-procedure main
create-array name=darray size=25 field=a:char
create-array name=wcarray size=100 field=a:char
do my_proc_darray( #d )
do my_proc_wcarray( #wc )
end-procedure

where my_proc.sqh looks like:

begin-procedure my_proc_{MY_ARRAY}( :#n )
move 0 to #n
input $line
while not isnull( $line )
   put $line into {MY_ARRAY}(#n) a
   add 1 to #n
end-while
end-procedure

Hope this helps.

Ray
----------------------------------------------------------------------
Ray Ontko       |  Ray Ontko & Co  |  "Ask me about Forte Software"
rayo@ontko.com  |  Richmond, In    |  See us at http://www.ontko.com/