Page 1 of 1 1
Topic Options
#72996 - 2003-02-09 05:59 AM Passing Var by Reference (sorta)
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
New Mexico Mark posted some interesting code in the ScheduleTask UDF thread.. I tried the concept in a new project and it's pretty cool. I thought I'd post an example here so it isn't buried in post 27 or so.

Basically, it allows you to pass the NAME of a variable to a function. The function can then convert the name to a variable and make a local copy. By adding a line of code that reverses the process, when the function exits, it copies its local copy back to the original.

Not quite as elegant as indirection, but useful nonetheless.

Glenn

code:
 
; example - red fish in, blue fish out
; Define simple array
$Seuss = "Red", "Fish"

; Display contents
"Seuss before fn: " + $Seuss[0] + " " + $Seuss[1] ?

; call the function, passing the NAME of the var
; note simple text string, no leading "$"
fn("Seuss")

; display contents after return from function
"Seuss after fn: " + $Seuss[0] + " " + $Seuss[1] ?


Function fn($ArgName)
; $ArgName contains the name of an external variable

; convert the arg name to an arg reference ("var" becomes "$var")
; remember that "$$" in a string becomes a single "$" when evaluated
; thus, the Execute "sees" ($Arg = $Seuss)
$RC = Execute('$$Arg="$$"+$ArgName')

; copy the received var to a local var
$RC = Execute('$$fArg=$Arg')

; display the contents of the local variable
"fn received: " + $fArg[0] + " " + $fArg[1] ?

; modify the local variable
$fArg[0] = "Blue"

; show the contents of the modified local var
"modified, fn contains: " + $fArg[0] + " " + $fArg[1] ?

; copy local var back to original
$RC = Execute('$Arg=$$fArg')

EndFunction

_________________________
Actually I am a Rocket Scientist! \:D

Top
#72997 - 2003-02-09 02:07 PM Re: Passing Var by Reference (sorta)
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Take a look at my HASH UDFs for some other code along these lines.

[ 09. February 2003, 14:09: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#72998 - 2003-02-09 03:49 PM Re: Passing Var by Reference (sorta)
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Holy Moly, Howard! I'm gonna have to run to 7-11 for a double-tall Columbian Supreme to decipher that on a Sunday morning. I can just hope it's what I think it is..

BTW - my reason for the concept above is:
I have a function that loads an array with default values. Another function takes user supplied values and places them into the correct location in the array. Finally, the array contents are passed to an external program.

I wanted to allow the programmer to specify his own array name, so that he could work with multiple data sets. Passing the name of his/her array to the function allows this.

An opinion - I'd like to see more "Seuss" code posted to illustrate ideas and concepts. Maybe a "cookbook' forum where short, example concepts could be presented & commented on. Would make a good place for the new folks to see how things are implemented - info that isn't in the manual. (It would also be a good place for all of us when we suffer from a bout of CRAFT*, CRAPS** or Coders Block!)

Glenn

* Can't Remember A Frigg'n Thing
** Can't Remember A Program Subroutine
_________________________
Actually I am a Rocket Scientist! \:D

Top
#72999 - 2003-02-09 04:05 PM Re: Passing Var by Reference (sorta)
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You can also achive something similar by defining a GLOBAL variable outside the UDF. Then, any modification of the variable inside the UDF will be reflected on the outside, too. However, being someone who likes clean (and elegant) code, I'd rather not define something GLOBAL for this purpose as I think that UDFs should not rely on any special outside information like GLOBAL variables.
_________________________
There are two types of vessels, submarines and targets.

Top
#73000 - 2003-02-09 04:26 PM Re: Passing Var by Reference (sorta)
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Unfortunately Jens the GLOBAL variable or array used inside a UDF is required until we can pass by reference.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#73001 - 2003-02-09 06:20 PM Re: Passing Var by Reference (sorta)
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Global? Maybe I'm not understanding your references.. I haven't defined anything as global, and the point of the example is to show that I can define a variable (regular or array) in the main program, pass it's name to a function, and have the function modify it. Granted, without a lot of Execute() code, it relies on making a copy for internal use and then copying it back to the original, but...

BTW, Howard - the caffiene has begun to flow. It IS what I thought, and an excellent (and USEFUL) example of using the Execute code. I'll be using it in the future, thanks! Oh, one thing - it's "associati-V-e" - a minor typo. [Big Grin]

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#73002 - 2003-02-09 06:27 PM Re: Passing Var by Reference (sorta)
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Corrected that typo a couple hours ago in the KiXtart.org UDF forum. The mirror sites will get updated shortly.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#73003 - 2003-02-09 06:34 PM Re: Passing Var by Reference (sorta)
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Glenn:

The GLOBAL is another way to be able to modify the content of a variable inside the UDF and have the results available outside the UDF:
code:
global $a
$a='whatever'
? $a
$rc=glbl()
? $a
exit 0
function glbl()
$a='changed inside glbl()'
endfunction

_________________________
There are two types of vessels, submarines and targets.

Top
#73004 - 2003-02-10 12:09 AM Re: Passing Var by Reference (sorta)
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
OK, sure.. but that assumes that the function knows the name of the external var, or - the name of the var is defined by the function.

So, while defining a var as global allows the function to modify it, it won't solve my problem, where the user defines the var name and wants the function to modify it. That's what the Suess code above does.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#73005 - 2003-02-10 01:12 AM Re: Passing Var by Reference (sorta)
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Glenn, did you consume sufficient amounts of caffine to work your way throught the HASH UDFs yet?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#73006 - 2003-02-10 10:26 AM Re: Passing Var by Reference (sorta)
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Glenn,
Howard is making the point that the "$Seuss" variable is global - this is the default scope for variables unless you "Dim" them.

If you "Dim" your $Seuss variable so that it is only visible in the local scope you will find that your function no longer works. This is also a problem for files included with "CALL".

That doesn't mean that using Execute() to evaluate variable names isn't any less useful, sometimes it is the only way to construct a UDF (see the example push/pop functions here)

You just need to be aware that the variables must have a global scope to manipulate them this way.

Until we have static variables and pass-by-reference we won't be able to create truly discreet functions.

[ 10. February 2003, 10:29: Message edited by: Richard H. ]

Top
#73007 - 2003-02-10 02:09 PM Re: Passing Var by Reference (sorta)
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
fnAllSpecialFolders() may provide another example for you.
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.056 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org