Sign in to follow this  
Followers 0
cappy2112

Const Arrays

22 posts in this topic

Does AutoIt support const arrays?

Share this post


Link to post
Share on other sites



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)

NEWHeader1.png

Share this post


Link to post
Share on other sites

right in help

8)

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.

Share this post


Link to post
Share on other sites

right in help

8)

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).

Share this post


Link to post
Share on other sites

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)


NEWHeader1.png

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

like i said... thats the hard way

and it doesn't want to work with all of the array functions available in Autoit

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

like i said... thats the hard way

and it doesn't want to work with all of the array functions available in Autoit

8)

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.


[center]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.[/center]

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

This error is due to a Byref on the _ArrayDisplay() parameter that should not be there. (Will be fixed in the next version

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)

Share this post


Link to post
Share on other sites

#13 ·  Posted (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 by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Efficiency. Copying arrays leads to huge performance bottlenecks.

Share this post


Link to post
Share on other sites

it gives the answer

dim'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

Share this post


Link to post
Share on other sites

#16 ·  Posted (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 by cappy2112

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Thanks but, This isn't an array

You'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.


[center]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.[/center]

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