AutoHotkey Community

It is currently May 27th, 2012, 1:06 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 

Should "Convert dynamic local variables to global" be a built-in function?
Yes
No
You may select 1 option

View results
Author Message
PostPosted: April 7th, 2008, 6:52 am 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
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.

_________________
Image


Last edited by Micahs on April 7th, 2008, 11:14 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 7th, 2008, 11:47 am 
Offline

Joined: February 5th, 2007, 12:19 pm
Posts: 192
Location: Osnabrück, Germany
I had to read this a few times to see this easy but genius idea. :D
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 7th, 2008, 11:11 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 7th, 2008, 11:33 pm 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Last edited by [VxE] on April 12th, 2008, 9:27 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 7th, 2008, 11:46 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
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.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon, sks and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group