Jump to content

Help to declare and assign many variables


Bresacon
 Share

Recommended Posts

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 by Bresacon
Link to comment
Share on other sites

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

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

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 by czardas
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 by Malkey
Wrong quote - sorry Subz
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...