Passing array elements by reference 
Author Message
 Passing array elements by reference

Is it at all possible, in Clipper 5.01, to pass array elements by
reference some way? It won't do it directly, unfortunately. And I don't
want to pass the entire array, because what I'm doing is something like
this:

function editrecord(scrfunc)
  local callstring, fields, n
  private getlist:={}

  if !rlock()
    return .f.
  endif

  callstring="{|f|"+scrfunc+"("
  fields={}
  for n=1 to fcount()
    aadd(fields, fieldget(n))
    if n!=1; callstring+=","; endif

  next
  callstring+=")}"

  dummy=eval(&(callstring), fields)
  read
  if lastkey()!=K_ESC
    for n=1 to fcount()
      fieldput(n, fields[n])
    next
  endif
  unlock; dbcommit()
return .t.

I can then call it with editrecord("custscreen") or
editrecord("prodscreen"), or any other screen function, and all that
screen function has to do is set up the getlist; all the framework (and
the real function contains a bit more prettifying stuff) is done in
editrecord().
I _could_ just pass the fields array to the screen function. But I would
like to be able to define the screen function like this:

custscreen(tname, taddress, tcity)



return

instead of having to do this:

custscreen(farray)



return

because that is, IMO, not nearly as pretty.

Since I want to use this for various possible records (just passing in
different screen functions), I can't just declare a random number of
variables to pass; I could do it if macro substitution allowed you to
declare things (I'd build the declaration the same way I build the call
string), but AFAICT it doesn't.
I might be able to get away with using macros for the temporary field
variables as well, and having it create a lot of privates called f1, f2,
and so forth, and using macros in a loop to unload them to the fields
again as well; but apart from using a lot of macros, creating
potentially a lot of privates isn't very elegant.
Is there any way to do this neatly? Or do I really need to pass an
array, or create a whole slew of automatically-private variables?

Richard



Sun, 01 Feb 2004 23:16:02 GMT  
 Passing array elements by reference

Quote:

> I _could_ just pass the fields array to the screen function. But I would
> like to be able to define the screen function like this:

> custscreen(tname, taddress, tcity)



> return

> instead of having to do this:

> custscreen(farray)



> return

I don't exactly understand what you want to do, but you can achive the
function custscreen as you want it using #xtranslate (in a .ch file so it is
"visible" to other .prg's). I don't think it really matters if you pass the
array to the function, but only a few of it's elements are actually used.

#xtranslate tname => farray[1]
#xtranslate taddress => farray[2]
#xtranslate tcity => farray[3]

custscreen(farray)



return

--
Luis Krause Mantilla
lkrausem at home dot com
luis_krause at hotmail dot com
"May the Source be with GNU"



Mon, 02 Feb 2004 06:27:15 GMT  
 Passing array elements by reference
Check you inbox .... I have sent you a data driven screen generator that
might give you some ideas.  It's written in 5.2 and uses an internal (is
_get_ in 5.01 I don't remember).

Good Luck!!!

--
::OutAHere()



Mon, 02 Feb 2004 10:47:46 GMT  
 Passing array elements by reference
On Thu, 16 Aug 2001 02:47:46 GMT, Sean Reiser

Quote:

>Check you inbox .... I have sent you a data driven screen generator that
>might give you some ideas.  It's written in 5.2 and uses an internal (is
>_get_ in 5.01 I don't remember).

>Good Luck!!!

>--
>::OutAHere()

Arrays are passed by reference by default in Clipper.


Mon, 02 Feb 2004 18:35:58 GMT  
 Passing array elements by reference

Quote:

> Arrays are passed by reference by default in Clipper.

Yes, _arrays_. I want to pass an array _member_ by reference. AFAICT, I
can't do it. I'm still deliberating which alternative hack to go for.

Richard



Mon, 02 Feb 2004 18:22:48 GMT  
 Passing array elements by reference

Quote:
> On Thu, 16 Aug 2001 02:47:46 GMT, Sean Reiser

> >Check you inbox .... I have sent you a data driven screen generator that
> >might give you some ideas. It's written in 5.2 and uses an internal (is
> >_get_ in 5.01 I don't remember).

> Arrays are passed by reference by default in Clipper.

ObCLCPedant: Not quite. It's more correct to say that array pointers are
passed by value. Arrays don't get passed, only pointers to arrays.

But, that's beside the point anyway, the OP was looking for ways to pass
elements of an array by reference, something you can't (easily?1) do.
Same goes for instance variables.

-----
1  I say that because I'd be willing to bet there is some awful low-level
hack that could be done to make it appear like this is happening <shudder>.

--
Dave Pearson                        |  OSLib - Timeslice release functions.
http://www.davep.org/               |     eg - Norton Guide reader for Linux.
http://www.davep.org/clipper/       |    weg - Norton Guide reader for Windows.
http://www.davep.org/norton-guides/ | dgscan - DGROUP scanner for Clipper.



Mon, 02 Feb 2004 19:47:08 GMT  
 Passing array elements by reference
Sean,

How are you? Long time no see.

  Eugene



Tue, 03 Feb 2004 04:43:49 GMT  
 Passing array elements by reference

Hi Rchard,

Congratulations you are heading in a right direction. Shifting the 'repeated
code' to a separate file makes lot of sense. The 'Edit/Append' functions of
a normal data entry screen definitely falls in this category. Removing it to
a different file will not only make the maintenance of the code easier, it
would also help  the 'look and feel' of the application to be consistent.

Key to achieve what you have listed below would be :

Instead of passing Array containing your 'GETs', create a code block
containing all the 'GETs' and pass this codeblock as a parameter to your
'edit' function. You can do the same for the 'SAY's'. Please remember to
declare the variables for all your fields that you want to 'GET' as 'File
wide STATIC' at start of your program.

I am assuming that codeblocks are available in 5.01

Please feel free to email me if you want any further clarification or a
sample code.

Good luck.
Tarun

Quote:

> Is it at all possible, in Clipper 5.01, to pass array elements by
> reference some way? It won't do it directly, unfortunately. And I don't
> want to pass the entire array, because what I'm doing is something like
> this:

> function editrecord(scrfunc)
>   local callstring, fields, n
>   private getlist:={}

>   if !rlock()
>     return .f.
>   endif

>   callstring="{|f|"+scrfunc+"("
>   fields={}
>   for n=1 to fcount()
>     aadd(fields, fieldget(n))
>     if n!=1; callstring+=","; endif

>   next
>   callstring+=")}"

>   dummy=eval(&(callstring), fields)
>   read
>   if lastkey()!=K_ESC
>     for n=1 to fcount()
>       fieldput(n, fields[n])
>     next
>   endif
>   unlock; dbcommit()
> return .t.

> I can then call it with editrecord("custscreen") or
> editrecord("prodscreen"), or any other screen function, and all that
> screen function has to do is set up the getlist; all the framework (and
> the real function contains a bit more prettifying stuff) is done in
> editrecord().
> I _could_ just pass the fields array to the screen function. But I would
> like to be able to define the screen function like this:

> custscreen(tname, taddress, tcity)



> return

> instead of having to do this:

> custscreen(farray)



> return

> because that is, IMO, not nearly as pretty.

> Since I want to use this for various possible records (just passing in
> different screen functions), I can't just declare a random number of
> variables to pass; I could do it if macro substitution allowed you to
> declare things (I'd build the declaration the same way I build the call
> string), but AFAICT it doesn't.
> I might be able to get away with using macros for the temporary field
> variables as well, and having it create a lot of privates called f1, f2,
> and so forth, and using macros in a loop to unload them to the fields
> again as well; but apart from using a lot of macros, creating
> potentially a lot of privates isn't very elegant.
> Is there any way to do this neatly? Or do I really need to pass an
> array, or create a whole slew of automatically-private variables?

> Richard

  Tarun.Mehra.vcf
< 1K Download


Tue, 03 Feb 2004 09:01:41 GMT  
 Passing array elements by reference
Hi Rchard,

Congratulations you are heading in a right direction. Shifting the 'repeated
code' to a separate file makes lot of sense. The 'Edit/Append' functions of
a normal data entry screen definitely falls in this category. Removing it to
a different file will not only make the maintenance of the code easier, it
would also help  the 'look and feel' of the application to be consistent.

Key to achieve what you have listed below would be :

Instead of passing Array containing your 'GETs', create a code block
containing all the 'GETs' and pass this codeblock as a parameter to your
'edit' function. You can do the same for the 'SAY's'. Please remember to
declare the variables for all your fields that you want to 'GET' as 'File
wide STATIC' at start of your program.

I am assuming that codeblocks are available in 5.01

Please feel free to email me if you want any further clarification or a
sample code.

Good luck.
Tarun

Quote:
> Is it at all possible, in Clipper 5.01, to pass array elements by
> reference some way? It won't do it directly, unfortunately. And I don't
> want to pass the entire array, because what I'm doing is something like
> this:

> function editrecord(scrfunc)
>   local callstring, fields, n
>   private getlist:={}

>   if !rlock()
>     return .f.
>   endif

>   callstring="{|f|"+scrfunc+"("
>   fields={}
>   for n=1 to fcount()
>     aadd(fields, fieldget(n))
>     if n!=1; callstring+=","; endif

>   next
>   callstring+=")}"

>   dummy=eval(&(callstring), fields)
>   read
>   if lastkey()!=K_ESC
>     for n=1 to fcount()
>       fieldput(n, fields[n])
>     next
>   endif
>   unlock; dbcommit()
> return .t.

> I can then call it with editrecord("custscreen") or
> editrecord("prodscreen"), or any other screen function, and all that
> screen function has to do is set up the getlist; all the framework (and
> the real function contains a bit more prettifying stuff) is done in
> editrecord().
> I _could_ just pass the fields array to the screen function. But I would
> like to be able to define the screen function like this:

> custscreen(tname, taddress, tcity)



> return

> instead of having to do this:

> custscreen(farray)



> return

> because that is, IMO, not nearly as pretty.

> Since I want to use this for various possible records (just passing in
> different screen functions), I can't just declare a random number of
> variables to pass; I could do it if macro substitution allowed you to
> declare things (I'd build the declaration the same way I build the call
> string), but AFAICT it doesn't.
> I might be able to get away with using macros for the temporary field
> variables as well, and having it create a lot of privates called f1, f2,
> and so forth, and using macros in a loop to unload them to the fields
> again as well; but apart from using a lot of macros, creating
> potentially a lot of privates isn't very elegant.
> Is there any way to do this neatly? Or do I really need to pass an
> array, or create a whole slew of automatically-private variables?

> Richard



Tue, 03 Feb 2004 08:18:15 GMT  
 Passing array elements by reference
Dear Friend

To pass arrays for reference i use the follow


Example

func main
Local x :={1,2,4}

read
return nil
function p(y)
if valtype(y)=="B"
    eval(y,200)
endif
return .t.



Tue, 03 Feb 2004 08:53:34 GMT  
 Passing array elements by reference
Tarun-

I agree with shifting 'repeated code'.  I tend to go one step further and
store the code in a database.  The real power of code blocks is the ability
to create efficient data driven applications.  I have applictions with one
get, one say, one tbrowse, one prompt/menu, etc.  All of these applications
are identical except for a couple of .prgs with the buisness logic in it
and of course the data dictionaries.  Once a person gets into this mindset
debugging is a snap!

--
::OutAHere()

When last we visited comp.lang.clipper Tarun Mehra

Quote:
> Hi Rchard,

> Congratulations you are heading in a right direction. Shifting the
> 'repeated code' to a separate file makes lot of sense. The
> 'Edit/Append' functions of a normal data entry screen definitely falls
> in this category. Removing it to a different file will not only make
> the maintenance of the code easier, it would also help  the 'look and
> feel' of the application to be consistent.

> Key to achieve what you have listed below would be :

> Instead of passing Array containing your 'GETs', create a code block
> containing all the 'GETs' and pass this codeblock as a parameter to
> your 'edit' function. You can do the same for the 'SAY's'. Please
> remember to declare the variables for all your fields that you want to
> 'GET' as 'File wide STATIC' at start of your program.

> I am assuming that codeblocks are available in 5.01

> Please feel free to email me if you want any further clarification or a
> sample code.

> Good luck.
> Tarun



Tue, 03 Feb 2004 12:10:23 GMT  
 Passing array elements by reference

Quote:

> Congratulations you are heading in a right direction. Shifting the 'repeated
> code' to a separate file makes lot of sense. The 'Edit/Append' functions of
> a normal data entry screen definitely falls in this category. Removing it to
> a different file will not only make the maintenance of the code easier, it
> would also help  the 'look and feel' of the application to be consistent.

Yes, I agree. I previously used an existing library/"RAD tool" for this;
well, RAD tool - it didn't call itself such, but it used much the same
"faster, easier, cheaper" lingo. Needless to say, it delivered none of
these. It was such a pain in the backside, and resulted in such ugly
screens to boot, that I've been writing my own replacement.

Quote:
> Instead of passing Array containing your 'GETs', create a code block
> containing all the 'GETs' and pass this codeblock as a parameter to your
> 'edit' function. You can do the same for the 'SAY's'. Please remember to
> declare the variables for all your fields that you want to 'GET' as 'File
> wide STATIC' at start of your program.

Ok, but this still results in my function seeing something else than
what appears to be the default temporary variables. In that case, I
might as well pass the array itself, and use #defines or #translates.

Ah well. I think I'll write a swift hack to at least get the job done,
and experiment with the various suggestions done here to see which I
like best.

Richard



Tue, 03 Feb 2004 16:54:03 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Access Array Elements by Arrays Reference

2. HOWTO: reference elements of an array if array name is in a variable

3. Shifting array element & regex on array element

4. Passing an array element as a parameter

5. access type referencing nested array element

6. Passing an array with unknown number of elements to a function

7. Passing array element to subroutine

8. Arrays: Build array in multiple for loops or replace array elements

9. How can I create an ActiveX IDL to pass an array of references into Labview

10. Are unconstrained arrays passed by references?

11. Pointer Arrays passed by Value/Reference?

12. Passing an array to a proc by reference

 

 
Powered by phpBB® Forum Software