Bresacon Posted December 23, 2016 Share Posted December 23, 2016 (edited) I need to declare a lot of variables as global and assign them later inside a function. I can do this with a variable already declared as global: $variable01 = "a123" But not this: $variable01 = "a123", $variable02 = "r123", $variable03 = "z456", $variable04 = "a789" It's very uncomfortable -to manage and aesthetically- to declare them one by one, each in one line. Is there any posibility to do ten in a line or something like that? I can't do the assignment of variables at the same time as declaration (yes, appending Global before the previous example works as expected), has to be in other place inside a function. Sorry if this is a silly question but I haven't found anything in the FAQ, the best coding practices or help file. Edited December 23, 2016 by Bresacon Link to comment Share on other sites More sharing options...
InunoTaishou Posted December 23, 2016 Share Posted December 23, 2016 I personally prefer to declare and assign variables one line at a time, in autoit, but to each their own Global $vVar1 = "1234", $vVar2 = "2345", $vVar3 = "3456", $vVar4 = "4567", $vVar5 = "5678", $vVar6 = "6789", _ $vVar7 = "7890", $vVar8 = "890", $vVar9 = "90" Link to comment Share on other sites More sharing options...
Subz Posted December 23, 2016 Share Posted December 23, 2016 I assume you mean something like this: Global $variable01, $variable02, $variable03, $variable04 Func _Variables() Dim $variable01 = "a123", $variable02 = "r123", $variable03 = "z456", $variable04 = "a789" EndFunc Link to comment Share on other sites More sharing options...
Bresacon Posted December 23, 2016 Author Share Posted December 23, 2016 1 hour ago, InunoTaishou said: I personally prefer to declare and assign variables one line at a time, in autoit, but to each their own Global $vVar1 = "1234", $vVar2 = "2345", $vVar3 = "3456", $vVar4 = "4567", $vVar5 = "5678", $vVar6 = "6789", _ $vVar7 = "7890", $vVar8 = "890", $vVar9 = "90" I wish I could do that but has to be separated. Link to comment Share on other sites More sharing options...
Bresacon Posted December 23, 2016 Author Share Posted December 23, 2016 1 hour ago, Subz said: I assume you mean something like this: Global $variable01, $variable02, $variable03, $variable04 Func _Variables() Dim $variable01 = "a123", $variable02 = "r123", $variable03 = "z456", $variable04 = "a789" EndFunc Yes, it will reuse the global definition but isn't the Dim keyword a not recommended one or something? Link to comment Share on other sites More sharing options...
Subz Posted December 23, 2016 Share Posted December 23, 2016 Its not recommended when declaring new variables, the variable should already be declared as either Local or Global before using Dim similar to how ReDim is used. Link to comment Share on other sites More sharing options...
czardas Posted December 23, 2016 Share Posted December 23, 2016 (edited) You could use an array. Perhaps something like this. Global $aVar[4] ; use a global array as a container Global Enum $00 = 0, $01, $02, $03 ; can be omitted because ... ; special variable names are not necessarily needed AssignElements() ; fill the array ; test the result ConsoleWrite($aVar[$00] & @CRLF & $aVar[$01] & @CRLF & $aVar[$02] & @CRLF & $aVar[$03] & @CRLF) Func AssignElements() $aVar = StringSplit( _ 'a123|r123|z456|a789' _ ; this data could also be replaced by a variable , '|', 2) EndFunc Edited December 23, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 23, 2016 Moderators Share Posted December 23, 2016 Bresacon, You asked: Quote Is there any posibility to do ten in a line or something like that? And then when shown that this was indeed possible: Global $vVar1 = "1234", $vVar2 = "2345", $vVar3 = "3456", $vVar4 = "4567", $vVar5 = "5678", $vVar6 = "6789", _ $vVar7 = "7890", $vVar8 = "890", $vVar9 = "90" say that: Quote I wish I could do that but has to be separated. So what exactly do you want? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted December 23, 2016 Share Posted December 23, 2016 I think Bresacon wants to separate declaration and assignment. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 23, 2016 Moderators Share Posted December 23, 2016 czardas, Light dawns - thanks. Although I still fail to see why only assigning one variable per line is difficult "to manage and aesthetically" - just put those lines inside a region and shrink it when not required: Global $variable01, $variable02, $variable03, $variable04 Func _Assign_Values() ; Other code #Region $variable01 = "a123" $variable02 = "r123" $variable03 = "z456" $variable04 = "a789" #EndRegion ; Other code EndFunc M23 itchiTrigger 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted December 23, 2016 Share Posted December 23, 2016 (edited) Yep, I agree that it's best to keep things straight forward and simple. I personally prefer to avoid using numerous global variables in my code. Edited December 23, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Malkey Posted December 23, 2016 Share Posted December 23, 2016 (edited) Testing Subz's Dim example of post #3, to my surprise, it works. And the help file explains why it works. Global $variable01, $variable02, $variable03, $variable04 _Variables() ConsoleWrite($variable01 & " " & $variable02 & " " & $variable03 & " " & $variable04 & @CRLF) Func _Variables() ; Dim = If the variable name already exist globally, it reuses the global variable - from AutoIt help. Dim $variable01 = "a123", $variable02 = "r123", $variable03 = "z456", $variable04 = "a789" EndFunc ;==>_Variables #cs Returns :- a123 r123 z456 a789 #ce Edited December 23, 2016 by Malkey Wrong quote - sorry Subz Link to comment Share on other sites More sharing options...
Bresacon Posted December 23, 2016 Author Share Posted December 23, 2016 11 hours ago, Subz said: Its not recommended when declaring new variables, the variable should already be declared as either Local or Global before using Dim similar to how ReDim is used. That's the confirmation I needed. Thanks everybody. This forum is superb. 11 hours ago, czardas said: You could use an array. Perhaps something like this. Global $aVar[4] ; use a global array as a container Global Enum $00 = 0, $01, $02, $03 ; can be omitted because ... ; special variable names are not necessarily needed AssignElements() ; fill the array ; test the result ConsoleWrite($aVar[$00] & @CRLF & $aVar[$01] & @CRLF & $aVar[$02] & @CRLF & $aVar[$03] & @CRLF) Func AssignElements() $aVar = StringSplit( _ 'a123|r123|z456|a789' _ ; this data could also be replaced by a variable , '|', 2) EndFunc I'll try that. Looks very tidy. 10 hours ago, Melba23 said: czardas, Light dawns - thanks. Although I still fail to see why only assigning one variable per line is difficult "to manage and aesthetically" - just put those lines inside a region and shrink it when not required: Global $variable01, $variable02, $variable03, $variable04 Func _Assign_Values() ; Other code #Region $variable01 = "a123" $variable02 = "r123" $variable03 = "z456" $variable04 = "a789" #EndRegion ; Other code EndFunc M23 I didn't know that trick, but I don't use SciTe. Laugh all you want, I always used the Notepad2 mod variant, am very comfortable in there. And lately I've been trying RJ TextEd -I like this one very much- and SublimeText. czardas 1 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