cappy2112 Posted January 14, 2006 Share Posted January 14, 2006 Does AutoIt support const arrays? Link to comment Share on other sites More sharing options...
Valuater Posted January 14, 2006 Share Posted January 14, 2006 right in help Dim / Global / Local / Const -------------------------------------------------------------------------------- Declare a variable, a constant, or create an array. Dim [Const]$variable [ = initializer ] Dim [Const] $array[subscript 1]...[subscript n] [ = initializer ] Parameters const [optional] If present, the Const keyword creates a constant rather than a variable. $variable The name of the variable to declare. initializer The value that will be initially assigned to the variable. A Const must include the initializer. subscript The number of elements to create for the array dimension, indexed 0 to n-1. 8) Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 right in help8)Yeah, I've already seen the docs , moe than once.I tried making a const array- it didn't compile.Actually, I was trying to make the const array global. Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 right in help8)Can you show an example of how to initialize a const array, with multiple elements, where each element has a different initializer value?The exact syntax is misleading from the docs (as usual). Link to comment Share on other sites More sharing options...
Valuater Posted January 14, 2006 Share Posted January 14, 2006 maybe this #include <Array.au3> Dim $avArray[8] $avArray[0] = 7 $avArray[1] = "Brian" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Christa" $avArray[5] = "Rick" $avArray[6] = "Jack" $avArray[7] = "Gregory" _ArrayDisplay( $avArray, "_ArrayDisplay() Test" ) Exit 8) Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 maybe this #include <Array.au3> Dim $avArray[8] $avArray[0] = 7 $avArray[1] = "Brian" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Christa" $avArray[5] = "Rick" $avArray[6] = "Jack" $avArray[7] = "Gregory" _ArrayDisplay( $avArray, "_ArrayDisplay() Test" ) Exit 8) Now try it with const- see what happens Link to comment Share on other sites More sharing options...
Valuater Posted January 14, 2006 Share Posted January 14, 2006 it gives the answer Can pass constants by reference only to parameters with "Const" keyword.: _ArrayDisplay( $avArray[1], "_ArrayDisplay() Test" ) _ArrayDisplay( ^ ERROR dim'ed arrays are changable... const'ed arrays are not ( too many rules ) AND... why fight it.. use dim??????????? 8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted January 14, 2006 Moderators Share Posted January 14, 2006 (edited) Global Const $MyAvArray_1 = "Brian" Global Const $MyAvArray_2 = "Jon" Global Const $MyAvArray_3 = "Larry" Global Const $MyAvArray_4 = "Christa" Global Const $MyAvArray_5 = "Rick" Global Const $MyAvArray_6 = "Jack" Global Const $MyAvArray_7 = "Gregory" Local $ShowConstants For $i = 1 To 7 Local $Const = Eval("MyAvArray_" & $i) $ShowConstants = $ShowConstants & $Const & @LF Next MsgBox(0, 'Constant Test', $ShowConstants) Exit Edit: Damn Code Tags!! Edit2: This way you can make Global/Local/Dim constants inside the For loop also. Edited January 14, 2006 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Valuater Posted January 14, 2006 Share Posted January 14, 2006 like i said... thats the hard way and it doesn't want to work with all of the array functions available in Autoit 8) Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted January 14, 2006 Moderators Share Posted January 14, 2006 like i said... thats the hard wayand it doesn't want to work with all of the array functions available in Autoit8)I'm not denying it's the hard way, and his approach is not one I would take or understand why it needs/wants to be taken.I was peddling around with it myself, and this was the only solution I could find for his situation. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Developers Jos Posted January 14, 2006 Developers Share Posted January 14, 2006 it gives the answer dim'ed arrays are changable... const'ed arrays are not ( too many rules ) AND... why fight it.. use dim??????????? 8) This error is due to a Byref on the _ArrayDisplay() parameter that should not be there. (Will be fixed in the next version This works fine : Const $avArray[7] = ["Brian", "Jon", "Larry", "Christa", "Rick", "Jack", "Gregory"] _ArrayDisplay( $avArray, "_ArrayDisplay() Test" ) ; Func _ArrayDisplay($avArray, $sTitle) Local $iCounter = 0, $sMsg = "" If (Not IsArray($avArray)) Then SetError(1) Return 0 EndIf For $iCounter = 0 To UBound($avArray) - 1 $sMsg = $sMsg & "[" & $iCounter & "] = " & StringStripCR($avArray[$iCounter]) & @CR Next MsgBox(4096, $sTitle, $sMsg) SetError(0) Return 1 EndFunc ;==>_ArrayDisplay SciTE4AutoIt3 Full installer Download page  - Beta files    Read before posting   How to post scriptsource   Forum etiquette Forum Rules  Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Valik Posted January 14, 2006 Share Posted January 14, 2006 This error is due to a Byref on the _ArrayDisplay() parameter that should not be there. (Will be fixed in the next versionRule #1 of working with arrays: Never pass an array by value. Just make the function definition take a Const ByRef array: Func _ArrayDisplay(Const ByRef $avArray, $sTitle) Link to comment Share on other sites More sharing options...
Developers Jos Posted January 14, 2006 Developers Share Posted January 14, 2006 (edited) Rule #1 of working with arrays: Never pass an array by value. Just make the function definition take a Const ByRef array: Func _ArrayDisplay(Const ByRef $avArray, $sTitle) @Valik, Is it just for efficiency not having to copy the Array in memory but just passing a pointer or is there also another reason ? Edited January 14, 2006 by JdeB SciTE4AutoIt3 Full installer Download page  - Beta files    Read before posting   How to post scriptsource   Forum etiquette Forum Rules  Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Valik Posted January 14, 2006 Share Posted January 14, 2006 Efficiency. Copying arrays leads to huge performance bottlenecks. Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 it gives the answerdim'ed arrays are changable... const'ed arrays are not ( too many rules )AND... why fight it.. use dim???????????8)Because I want a CONST array Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 (edited) Global Const $MyAvArray_1 = "Brian" Global Const $MyAvArray_2 = "Jon" Global Const $MyAvArray_3 = "Larry" Global Const $MyAvArray_4 = "Christa" Global Const $MyAvArray_5 = "Rick" Global Const $MyAvArray_6 = "Jack" Global Const $MyAvArray_7 = "Gregory" Local $ShowConstants For $i = 1 To 7 Local $Const = Eval("MyAvArray_" & $i) $ShowConstants = $ShowConstants & $Const & @LF Next MsgBox(0, 'Constant Test', $ShowConstants) Exit Edit: Damn Code Tags!! Edit2: This way you can make Global/Local/Dim constants inside the For loop also. Thanks but, This isn't an array Edited January 14, 2006 by cappy2112 Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 Efficiency. Copying arrays leads to huge performance bottlenecks.A good reason why ALL arguments should be implicitly passed ByRef, unless the ByVal keyword is explicitly used. Link to comment Share on other sites More sharing options...
cappy2112 Posted January 14, 2006 Author Share Posted January 14, 2006 This works fine : Const $avArray[7] = ["Brian", "Jon", "Larry", "Christa", "Rick", "Jack", "Gregory"] This is what I was after. Although I wanted to use enums as indices in the assignnet, but I don't think that will work. No problem. I'll declare it like this and just use the enums to get the values of the consts. Thanks! Link to comment Share on other sites More sharing options...
Valik Posted January 15, 2006 Share Posted January 15, 2006 A good reason why ALL arguments should be implicitly passed ByRef, unless the ByVal keyword is explicitly used.Wrong. Only arrays and parameters that need to be modified should be ByRef, everything else should be done by value. If you go around arbitrarily making everything ByRef, then you will always have to pass a variable to a function and won't be able to pass a literal: Local $temp = "Must use temporary variable." Function1($temp) Func Function1(ByRef $value) MsgBox(4096, "", $value) EndFunc Versus: Function2("This works without the temporary.") Func Function2($value) MsgBox(4096, "", $value) EndFunc Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted January 15, 2006 Moderators Share Posted January 15, 2006 Thanks but, This isn't an arrayYou're right in the traditional sense, but if you can't accomplish what you want with an array, this will work as a nasty work around. Open your mind up a bit, look at your array, look at my example, and then take it from there. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. 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