Sign in to follow this  
Followers 0
Physical

Help understanding Dim

18 posts in this topic

I read the online documentation about it and i understand that its used for declaring things. However what i do not understand is why i have to use it to declare an array instead of being able to declare that array globaly.

This is what i tried to do:

Global $Paused, $TCoord, $Colors, $X

$Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]
    For $X = 0 to Ubound($Colors) - 1
        $TCoord = PixelSearch ( 75, 150, 950, 600, $Colors[$X], 10)
        If NOT @error Then
            Action()
            ExitLoop
        EndIf
    NextoÝ÷ Ù©Ý
-j̧µ¬^®º+N¬ÈhÂ'âÅçbµ«­¢+Ù±½°ÀÌØíAÕÍ°ÀÌØíQ
½½É°ÀÌØí`()¥´ÀÌØí
½±½ÉÍlÑtôlÁàÕÈÔÄÜ°ÁàÔÔÌàÈØ°ÁàÐØÌÌÉ°ÁàÝÕÐÕt(%½ÈÀÌØí`ôÀѼU½Õ¹ ÀÌØí
½±½É̤´Ä($$ÀÌØíQ
½½ÉôA¥á±MÉ  ÜÔ°ÄÔÀ°äÔÀ°ØÀÀ°ÀÌØí
½±½ÉÍlÀÌØíat°ÄÀ¤($%%9=PÉɽÈQ¡¸($$%Ñ¥½¸ ¤($$%á¥Ñ1½½À($%¹%(%9áÐ

If you notice the Dim before the $Colors array. I did this because i saw someone else do it in a forum post. My question is why did i have to do it that way? Why can't i declare it with the rest of the globals? Thx for any help

Share this post


Link to post
Share on other sites



hey

you wrote: Global $colors, but you've forgotten the []. You must enter this value, because this sets the amount of items available in a array.

Global $colors[20] = 20 arrays available ($colors[1], $colors[2], $colors[0])

if you write

Global $Paused, $TCoord, $Colors[20], $X

your code will be fine i think. dont have autoit here.

notice that i wrote [20], you can still modify this.

immense

Share this post


Link to post
Share on other sites

This is because when declaring arrays, the number of indices is STATIC and cannot be changed at runtime (unless you re-declare the array.) This is just the nature of arrays.

When you declare a variable, if you don't specify the square brackets AutoIt assumes the declaration to be that of a standard variant type. That's why you must specify the square brackets with the number of indices to indicate to AutoIt this is an array and you need multiple memory locations.

So you can do something like...

Global $Colors[4]       ; Declare Colors as an array

$Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]   ; Assign values to the array

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This is because when declaring arrays, the number of indices is STATIC and cannot be changed at runtime (unless you re-declare the array.) This is just the nature of arrays.

When you declare a variable, if you don't specify the square brackets AutoIt assumes the declaration to be that of a standard variant type. That's why you must specify the square brackets with the number of indices to indicate to AutoIt this is an array and you need multiple memory locations.

So you can do something like...

Global $Colors[4]       ; Declare Colors as an array

$Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]   ; Assign values to the array
Thx for the help. I tried what u said, i even remade a whole new script with only the 2 lines of code you just wrote. It still gives me the exact same syntax error as it did before i put the Dim in front of it. I still don't understand why.

ERROR: syntax error

$Colors[4] = [

~~~~~~~~^

Edited by Physical

Share this post


Link to post
Share on other sites

Thx for the help. I tried what u said, i even remade a whole new script with only the 2 lines of code you just wrote. It still gives me the exact same syntax error as it did before i put the Dim in front of it. I still don't understand why.

ERROR: syntax error

$Colors[4] = [

~~~~~~~~^

The Dim keyword means reusing an existing variable or redeclaring it.If it's used without the array declaration,it 'imports' a global variable into the function,and if it does not exist,create a global one.Using Dim with an array notation means redeclaring the existing variable.You can not change the arrays size without redeclaring it with Dim,Redim,Global or Local,but you can assign values to it just by using $Array = [1,2,3,4].

Share this post


Link to post
Share on other sites

errr........ okay, answer to the second question (sigh)

look the pointer (^) where the error lies.

it isnt the array, but it's the value of the array.

instead starting the value with [, use "" (brackets)

Global $Colors[4] ; Declare Colors as an array

$Colors[4] = "[0x5C2517, 0x553826, 0x46332B, 0x7D5A45]" ; Assign values to the array

immense

Share this post


Link to post
Share on other sites

errr........ okay, answer to the second question (sigh)

look the pointer (^) where the error lies.

it isnt the array, but it's the value of the array.

instead starting the value with [, use "" (brackets)

Global $Colors[4] ; Declare Colors as an array

$Colors[4] = "[0x5C2517, 0x553826, 0x46332B, 0x7D5A45]" ; Assign values to the array

immense

Just try what i said and tell me the result:
$Colors = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]

Share this post


Link to post
Share on other sites

errr........ okay, answer to the second question (sigh)

look the pointer (^) where the error lies.

it isnt the array, but it's the value of the array.

instead starting the value with [, use "" (brackets)

Global $Colors[4] ; Declare Colors as an array

$Colors[4] = "[0x5C2517, 0x553826, 0x46332B, 0x7D5A45]" ; Assign values to the array

immense

Did you try that before you posted it? :)

The quotation marks do not belong around the brackets:

#include <Array.au3>; Only for _ArrayDisplay()

Global $Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]  ; Assign values to the array

_ArrayDisplay($Colors, "$Colors")

The Dim keyword is deprecated, and may go away some day. It is preferred to declare scope explicitly with Global or Local.

The size of the array can be set with a variable:

#include <Array.au3>; Only for _ArrayDisplay()

Global $iCount = InputBox("Test", "Enter number of elements for array: ")
If Not StringIsInt($iCount) Then Exit

Global $Colors[$iCount] 
; Assign values to the array
For $n = 0 To UBound($Colors) - 1
    $Colors[$n] = "0x" & Hex(Random(0, 2^24 - 1, 1), 6)
Next

_ArrayDisplay($Colors, "$Colors")

The variable does not have to be declared as an array where its type will be changed in usage later:

#include <Array.au3>; Only for _ArrayDisplay()

Global $Colors = "Dingleberries"; String, not an array
$Colors = StringSplit("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F", ","); Now its an array

_ArrayDisplay($Colors, "$Colors")

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

I never knew Dim was deprecated,so if I want to make sure a Global variable is declared inside the function can I use Global?Will the variable be re-declarated and the value lost or will it be kept?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Just try what i said and tell me the result:

$Colors = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]
Ok thankyou all very much for the help. I am sorry i am not trying to be difficult but i have tried everything all of you have said and i still either get syntax error, or an error when i try to print it to a msg box to test it out.

Global $Colors[4] 

$Colors = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]oÝ÷ Ú¯zƬÊ{ZÅê뢸î²ÛazX§y«­¢+ØÀÌØí
½±½ÉÌôlÁàÕÈÔÄÜ°ÁàÔÔÌàÈØ°ÁàÐØÌÌÉ°ÁàÝÕÐÕtoÝ÷ Ú¯zËazƦz̧µ¬^®º+jëh×6$Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]oÝ÷ ÚƦyê뢼V¬ç$Â¥v¯z+p)^¬·*baƧvÚòx½êò¦,¹^rv­â~§vÊ&zØbajÜ(®Gjëh×6Global $Colors[4] = [0x5C2517, 0x553826, 0x46332B, 0x7D5A45]

MsgBox (0, "test", $Colors[1])oÝ÷ Ú[m«lr¸©·
+Â0®^¶¬)â±Êâ¦Ûhµë-Ø­Â)(!µÉbrJ'¶¬r·'âµø§vÆ¢Z+«)uè­-,!×¢·¥¸ ×%É(ØZ¶±zV§vØ^)º¦²èÅ«­¢+Ù±½°ÀÌØí
½±½ÉÍlÑtôlÁá°ÁáÀ°ÁáÀÀ°ÁáÀÀÁt°ÀÌØíQ
½½É)!½Ñ-åMÐ ÅÕ½Ðíí!=5ôÅÕ½Ðì°ÅÕ½ÐíQÍÐÅÕ½Ðì¤()]¡¥±Ä(%M±À ÄÀÀ¤)]¹()Õ¹QÍÐ ¤(%½ÈÀÌØí`ôÀѼU½Õ¹ ÀÌØí
½±½É̤´Ä($$ÀÌØíQ
½½ÉôA¥á±MÉ  ÌÀÜ°ÈÈÀ°ÜÈÀ°ÔÄÀ°ÀÌØí
½±½ÉÍlÀÌØíat°ÄÔ¤($%%9=PÉɽÈQ¡¸($$%Ñ¥½¸ ¤($$%á¥Ñ1½½À($%¹%(%9áÐ)¹Õ¹()չѥ½¸ ¤(%5½ÕÍ
±¥¬ ÅÕ½ÐíÉ¥¡ÐÅÕ½Ðì°ÀÌØíQ
½½ÉlÁt°ÀÌØíQ
½½ÉlÅt°Ä¤(%5Í   ½à À°ÅÕ½ÐíÑÍÐÅÕ½Ðì°ÀÌØí
½±½ÉÍlÀÌØíat¤)¹Õ¹

I think i understand now. Thanks very much all of you for your help

Edit: i typed this before i saw PSaltyDS's reply. Thankyou very much for explaining it.

Edited by Physical

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I never knew Dim was deprecated,so if I want to make sure a Global variable is declared inside the function can I use Global?Will the variable be re-declarated and the value lost or will it be kept?

Global variables values can be changed inside functions, but should never be declared inside functions. This fails syntax check, though it does run from SciTE:
_Test()
MsgBox(64, "Results", "$GlobalVar = " & $GlobalVar)

Func _Test()
    Global $GlobalVar = "Global"
    Local $GlobalVar = "Local"
EndFunc

:)

Edit: Corrected MsgBox().

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Global variables values can be changed inside functions, but should never be declared inside functions. This fails syntax check and won't run from SciTE:

_Test()
MsgBox(64, "Results", "$GlobalVar = " & $GlobalVar & "  $LocalVar = " & $LocalVar)

Func _Test()
    Global $GlobalVar = "Global"
EndFunc

:)

I sorted it out,it works as expected,and now i have to do a search-replace into all my Dims:

Global $Var = 1

Change()

;Var should be 2
MsgBox(0,0,$Var)
Func Change()
    Global $Var
    $Var = 2
EndFunc

Share this post


Link to post
Share on other sites

I sorted it out,it works as expected,and now i have to do a search-replace into all my Dims:

Global $Var = 1

Change()

;Var should be 2
MsgBox(0,0,$Var)
Func Change()
    Global $Var
    $Var = 2
EndFuncoÝ÷ Ûú®¢×z··öƧ¢Ø^­ìZ^jëh×6Global $azTest[4] = ["red", "white", "blue", "green"], $sString
For $i = 0 to 3
    $sString &= "$azTest["&$i&"] = " & $azTest[$i] & @CRLF
Next
MsgBox(0, "Test", $sString)

_ChangeArray()
For $i = 0 to 3
    $sString &= "$azTest["&$i&"] = " & $azTest[$i] & @CRLF
Next
MsgBox(0, "Test", $sString)

Func _ChangeArray()
    $sString = ""
    Global $azTest[4] = ["purple", "pink", "orange", "violet"]
EndFunc

I was bored, eh.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

I sorted it out,it works as expected,and now i have to do a search-replace into all my Dims:

Global $Var = 1

Change()

;Var should be 2
MsgBox(0,0,$Var)
Func Change()
    Global $Var
    $Var = 2
EndFunc
Huh? :)

What is the point of using the Global keyword (or Dim, for that matter) inside the function there?

:(

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Huh? :)

What is the point of using the Global keyword (or Dim, for that matter) inside the function there?

:(

You get a syntax error if you don't

EDIT: If it is an array, that is.

Edited by KentonBomb

Share this post


Link to post
Share on other sites

You get a syntax error if you don't

EDIT: If it is an array, that is.

But if it's already declared, you are only using the variable inside the function, why have the global keyword inside there at all?

And what does it have to do with arrays? It is the same for strings or numbers.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

UGH! I\'m such an IDIOT! :)

The problem with my code is that the statement

Global $Colors[4]       ; Declare Colors as an arrayoÝ÷ ۥתÞjv«­¬°ØE9DbØzÍ7è*%¢»4¶èºtß ¨ìÝ8^ìm²Ö­zg§µ«­¢+ØÀÌØí
½±½ÉÍlÑtôlÁàÕÈÔÄÜ°ÁàÔÔÌàÈØ°ÁàÐØÌÌÉ°ÁàÝÕÐÕtìÍÍ¥¸Ù±ÕÌѼѡÉÉäoÝ÷ Ú+"ö¥×è­§(º^¡úÞjÊ'³R4÷mýÚòx-¢)â¶&¥74G¥zg§¶íæ«­¬°ØE9Doj[±©ÝØ=ÛfºÈ§©âö¥Ø§uìfiݦiÝ¡ÈZ¢¶ÚºÚ"µÍÛØ[ ÌÍÐÛÛÜÖÍHHÌ
PÌLMË
MLÎ

ÌÌ
Ñ
PM
WHÈXÛH[[][X^HH^

and it will work.

Edited by ACS

Share this post


Link to post
Share on other sites

Thankyou all of you for your help. I really appreciate it. I guess i am not the only one who didn't completely understand Dim.

Share this post


Link to post
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
Sign in to follow this  
Followers 0