AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Convert dynamic local variables to global variables

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  

Should "Convert dynamic local variables to global" be a built-in function?
Yes
100%
 100%  [ 2 ]
No
0%
 0%  [ 0 ]
Total Votes : 2

Author Message
Micahs



Joined: 01 Dec 2006
Posts: 338

PostPosted: Mon Apr 07, 2008 6:52 am    Post subject: Convert dynamic local variables to global variables Reply with quote

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
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Mon Apr 07, 2008 11:47 am    Post subject: Reply with quote

I had to read this a few times to see this easy but genius idea. Very Happy
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
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 338

PostPosted: Mon Apr 07, 2008 11:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1130

PostPosted: Mon Apr 07, 2008 11:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 338

PostPosted: Mon Apr 07, 2008 11:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group