Jump to content

Recommended Posts

Posted (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 by Bresacon
Posted

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"

 

Posted
  On 12/23/2016 at 5:43 AM, 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"

 

Expand  

I wish I could do that but has to be separated.

 

Posted
  On 12/23/2016 at 6:04 AM, 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

 

Expand  

Yes, it will reuse the global definition but isn't the Dim keyword a not recommended one or something?

Posted

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.

Posted (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 by czardas
  • Moderators
Posted

Bresacon,

You asked:

  Quote

 Is there any posibility to do ten in a line or something like that?

Expand  

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.

Expand  

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:

  Reveal hidden contents

 

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted (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 by Malkey
Wrong quote - sorry Subz
Posted
  On 12/23/2016 at 7:48 AM, 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.

Expand  

That's the confirmation I needed.

Thanks everybody. This forum is superb. ;)

 

  On 12/23/2016 at 8:11 AM, 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

 

Expand  

I'll try that. Looks very tidy.

 

  On 12/23/2016 at 8:45 AM, 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

Expand  

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.

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
  • Recently Browsing   0 members

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