Exit Posted June 7, 2019 Share Posted June 7, 2019 How can you remove a global variable that has been set in a function so that it is no longer visible in another function? In the following example, $ i = 4 should not have $ x = 0 and Scope = Global, but $ x = and Scope = Local Does anyone has a good idea or is it impossible? Opt("ExpandVarStrings", 1) For $i = 1 To 4 _test1($i) _test2($i) Next Func _test1($i) Global $x If $i > 2 Then Local $x ; if $i greater than 2 then switch to local $x If $i= 4 Then Global $x = 0 ; try to kill global. ########## does not function ################# EndIf ; How To disale/delete/kill global $x so it does not exist in function test2() as global anymore ? ; how to force it to be Scope=local? ; function test2() should not be modified. $x += 1 ConsoleWrite("i=$i$ T1x=$x$ Scope: " & (IsDeclared("x") ? (IsDeclared("x") = 1 ? "Global" : "Local ") : "None")) EndFunc ;==>_test1 Func _test2($i) Dim $x ConsoleWrite(" T2x=$x$ Scope: " & (IsDeclared("x") ? (IsDeclared("x") = 1 ? "Global" : "Local ") : "None") & @LF) EndFunc ;==>_test2 App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
BrewManNH Posted June 7, 2019 Share Posted June 7, 2019 That is some pretty terrible looking code you posted there. First off, you can't have a Global and a Local variable with the same name and expect it to not collide in hideous ways. In your code, you have a global declaration of $x, and in the same function you have a Local declaration of the same variable name. Because there's a local version of that variable, that is the one you will be referring to inside that function. That's how these things work and you won't get it changed. Next in test 2 you used Dim to redeclare the same variable name, because the variable is a global variable, you just redeclared the global variable, because that's exactly how Dim works. This is a good example of why having Global variables can cause problems later on in a script. Here's your script rewritten to demonstrate exactly what's going on. ;~ Declare a variable $x in the global scope Global $x = 10 For $i = 1 To 4 _test1($i) _test2($i) Next Func _test1($i) ;~ declare a variable, also called $x, in the local scope Local $x If $i > 2 Then ; uninformed comment removed ;~ set the value of the local variable $x to zero if $i > 4, please note, previous to this $x was never initialized with a value If $i = 4 Then $x = 0 EndIf ; How To disable/delete/kill global $x so it does not exist in function test2() as global anymore ? ; how to force it to be Scope=local? ; function test2() should not be modified. $x += 1 ConsoleWrite("i=" & $i & " T1 $x=" & $x & " Scope: " & (IsDeclared("x") ? (IsDeclared("x") = 1 ? "Global" : "Local ") : "None")) EndFunc ;==>_test1 Func _test2($i) ;~ This will redeclare the Global variable $x and initialize it with a new value, NOTE: using Dim without an initializer value will do nothing to the Global value Dim $x = 111 ConsoleWrite(" T2 $x=" & $x & " Scope: " & (IsDeclared("x") ? (IsDeclared("x") = 1 ? "Global" : "Local ") : "None") ) Dim $x ConsoleWrite(" T22 $x=" & $x & " Scope: " & (IsDeclared("x") ? (IsDeclared("x") = 1 ? "Global" : "Local ") : "None") & @LF) EndFunc ;==>_test2 So, to answer your initial question, no you can't change the value of a Global variable if you have reused the variable name and declared it local in a function. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Exit Posted June 7, 2019 Author Share Posted June 7, 2019 @BrewManNH Quote That is some pretty terrible looking code you posted there. Strong statement. I would have expected a milder expression from a MVP. If you mean using 'pretty terrible looking code' to opt ("ExpandVarStrings", 1), then I'll have to argue. Opt ("ExpandVarStrings", 1) is a wonderful feature for write-lazy encoders. I am one of them. Here is a short example: Opt("ExpandVarStrings", 1) ; I like lazy typing even when it's terrible looking. ConsoleWrite(" Timestamp: @year@:@mon@:@mday@ @hour@.@min@.@sec@@lf@") ; my lazy typing :-) ConsoleWrite(" Timestamp: " & @YEAR & ":" & @MON & ":" & @MDAY & " " & @HOUR & "." & @MIN & "." & @SEC & @LF) ; good looking typing. #cs Here is the output of the script: Timestamp: 2019:06:07 21.55.43 Timestamp: 2019:06:07 21.55.43 #ce Quote First off, you can't have a Global and a Local variable with the same name and expect it to not collide in hideous ways. In your code, you have a global declaration of $x, and in the same function you have a Local declaration of the same variable name. Because there's a local version of that variable, that is the one you will be referring to inside that function. That's how these things work and you won't get it changed. Close miss. It is quite possible to use global and local variables with the same name in a function. The next example alternately uses global and local. Note that the global value of $x increases every second execution while the local value remains always 1 in between. Conclusion: It is quite possible to use global and local variables with the same name in a function. Opt("ExpandVarStrings", 1) ; I like lazy typing even when it's terrible looking. For $i = 1 To 9 _test1($i) Next Func _test1($i) Global $x If Mod($i,2) = 0 Then Local $x $x += 1 ConsoleWrite("i=$i$ x=$x$ Scope: " & (IsDeclared("x") = 1 ? "Global" : "Local ")&@lf) EndFunc ;==>_test1 #cs Here is the output of the script: i=1 x=1 Scope: Global i=2 x=1 Scope: Local i=3 x=2 Scope: Global i=4 x=1 Scope: Local i=5 x=3 Scope: Global i=6 x=1 Scope: Local i=7 x=4 Scope: Global i=8 x=1 Scope: Local i=9 x=5 Scope: Global #ce Quote Here's your script rewritten to demonstrate exactly what's going on. Unfortunately, the script does not hit the problem. I do not want a global definition outside of the function, but I want to delete the global definition of $x from within the function. But this is apparently not possible. App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
BrewManNH Posted June 7, 2019 Share Posted June 7, 2019 4 minutes ago, Exit said: Conclusion: It is quite possible to use global and local variables with the same name in a function. Because you're redeclaring it as local inside the function every other iteration of the function call, just as I explained above. In the first call to the function, local $x isn't declared, so it's using the Global, the second call to the function is declaring a local variable with the same name so it uses the local. Every time you leave the function, the Local variable is destroyed, so it's no longer declared when you go back into it, until you declare it again. I don't see why that's hard to understand. 8 minutes ago, Exit said: Strong statement. I would have expected a milder expression from a MVP. If you mean using 'pretty terrible looking code' to opt ("ExpandVarStrings", 1), then I'll have to argue. Strong statement because it's true. Also, my comment had nothing to do with the Opt you used, that's just one way of doing things, that didn't really need to be done, as shown above. The terrible part was the point that you declared a Global inside a function, and then tried to use Global inside your If statement. Also thinking that a local variable will "kill" a global that way isn't killing anything, it's just giving a new value to your Global variable, and won't affect a local variable. This would have worked, but would still have been wrong to do it. If $i= 4 Then Global $x = 0 EndIf But, as the question has been answered, I'll leave it at this. FrancescoDiMuro and JLogan3o13 2 If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
argumentum Posted June 7, 2019 Share Posted June 7, 2019 1 hour ago, Exit said: I do not want a global definition outside of the function how about Local Static $var = "x" ? Exit 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Exit Posted June 7, 2019 Author Share Posted June 7, 2019 (edited) After some research I found out that it is not possible to remove variables in C ++. You can only set them to "NULL". Therefore, it is probably also not possible in Autoit. I'm used to remove variables in powershell scripts where the command would be: PS> Remove-variable x But the tip of @argumentum can help me further. Thanks. Edited June 7, 2019 by Exit typo argumentum 1 App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
argumentum Posted June 8, 2019 Share Posted June 8, 2019 8 minutes ago, Exit said: After some research I found out that it is not possible to remove variables I have no idea of just why you'd want to do that but maybe you can use something like this ? Func MyLocalStaticVar($var = Default) Local Static $myVar If $var = Default Then Return $myVar $myVar = $Var Return $myVar EndFunc Exit 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now