| View previous topic :: View next topic |
| Should "Convert dynamic local variables to global" be a built-in function? |
| Yes |
|
100% |
[ 2 ] |
| No |
|
0% |
[ 0 ] |
|
| Total Votes : 2 |
|
| Author |
Message |
Micahs
Joined: 01 Dec 2006 Posts: 338
|
Posted: Mon Apr 07, 2008 6:52 am Post subject: Convert dynamic local variables to global variables |
|
|
This came about because I wanted to have dynamic variables created in a function accessible in other areas without making the func global. I wanted to avoid creating many unnecessary global vars that might be used only once. I also didn't want to bother clearing all those vars to free up resources. On the other hand, I could declare all vars except the desired global ones local, but I'm too lazy to declare each var used, and some are dynamic themselves. It also can help to avoid creating many unique var names. For my purposes, this takes care of a fairly common issue.
Also, I thought that this might help with combining several scripts. I'm not sure how it could be implemented yet. It'll take some testing to see how useful it will be, if at all.
| Code: | ;Convert a dynamic local variable to a global variable
;EXAMPLE: In this case the result is like: g_1_localvar, g_2_localvar, Etc
_var = g
function(_var)
loop,4 ;this happens with vars in a global scope
{ tmp := _var "_" A_Index "_localvar"
tmp1 .= tmp " - " %tmp% " but now it's a global variable!`n"
}
msgbox,%tmp1%
Return
function(_num)
{
loop,4 ;this all happens with vars local to this func
{ %_num%_%A_Index%_localvar = This used to be a local variable,
globalWrapper(_num "_" A_Index "_localvar", %_num%_%A_Index%_localvar)
}
}
globalWrapper(victim, content) ;this turns a dynamic local variable into a global variable
{
global
%victim% := content
}
| Of course, this example is more complex than it needs to be, but I think that it displays the concept well. _________________

Last edited by Micahs on Mon Apr 07, 2008 11:14 pm; edited 1 time in total |
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Mon Apr 07, 2008 11:47 am Post subject: |
|
|
I had to read this a few times to see this easy but genius idea.
So here is a perhaps easier example:
| Code: |
testfunction()
msgbox,%testfunction_localvar_testglobal% `n%testglobal%
Return
testfunction()
{
testlocal=this was local
globalWrapper("testglobal", testlocal)
globalWrapper(A_ThisFunc "_localvar_testglobal", testlocal)
}
globalWrapper(NameOfTheGlobalVar, LocalVar) ;this turns a dynamic local variable into a global variable
{
global
%NameOfTheGlobalVar% := LocalVar
} |
You function will be very useful.
Thanks. |
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 338
|
Posted: Mon Apr 07, 2008 11:11 pm Post subject: |
|
|
Yeah, I was trying to demonstrate the 'dynamic' part and maybe got a little carried away. A simpler example might have been better. Thanks!
I don't think this would have too much overhead or be difficult to build in to AHK. I wonder if there would be enough interest for Chris to add it to the built-in functions?
I added a poll to see if there is any interest. _________________
 |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1130
|
Posted: Mon Apr 07, 2008 11:33 pm Post subject: |
|
|
That function is probably worth being included in a library. I had written a very similar function for one of my WIPs, here's the skinny: | Code: | PassGlobal( VarName, Value = "PG_FLAG_SET_NULL")
{ ; for passing data between a non-global function and a global variable
; Global
Local vtemp
vtemp := %VarName%
If Value != PG_FLAG_SET_NULL
%VarName% := Value
return % vtemp
} |
notice that it will work both ways. It can either get a value from a dynamic global variable name, set a value to the same, or both at once. Not bad, eh?
[edit:] Minor tweak, marked in red, just in case this thread ever gets resurrected. _________________ My Home Thread
More Common Answers: 1. It's in the FAQ 2. Ternary ( ? : ) guide 3. Post code with [code][/code] tags
Last edited by [VxE] on Sat Apr 12, 2008 9:27 pm; edited 1 time in total |
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 338
|
Posted: Mon Apr 07, 2008 11:46 pm Post subject: |
|
|
Cool. I tried to add a poll option for "No, but this (or something like this) should be included in the standard library." but once a vote has been cast it can't be modified.
Thanks. The two-way option is a nice feature. _________________
 |
|
| Back to top |
|
 |
|