Arion

Arrays

36 posts in this topic

I have been trying to get my application to work with arrays, yet it seems like most of the functions that work with arrays either require numeric indices, or the help pages are so vague as to be almost useless. Am I right, is AutoIt not equipped to deal with string indices properly or am I just reading everything wrong?


[quote]“Programming is like *ex: one mistake and you’re providing support for a lifetime.”(Michael Sinz)[/quote] [quote]“There are two ways to write error-free programs; only the third one works.”(Alan J. Perlis)[/quote]

Share this post


Link to post
Share on other sites



Arion,

As far as I know you need numeric indices in arrays. Could you give an example of a "string" index which does not work? It might be as simple as using Number to convert it to the correct type. :graduated:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

I don't think you can replace the index numbers, but you can use a two dimensional array and use column 0 for a string type index. But you would have to include more code to get it to behave like an index (a kind of work around). That's if I understand you correctly.

Share this post


Link to post
Share on other sites

Arion,

As far as I know you need numeric indices in arrays. Could you give an example of a "string" index which does not work? It might be as simple as using Number to convert it to the correct type. :graduated:

M23

To be honest, I put my other project on the back burner in order to write a registry UDF to make all this variable nonsense easier for people. As for the example:

In my actual registry variable, I have the main var, and then secondary containers I call ACVs (Access Control Variables), which should be s string.

Global $Registry[2] = [0, 'Registry']

Whereas registry should be the key, not the value (Yes, I know the way it is written it comes up as the value).

I guess what I'm wondering is if there is a way to write it somewhat like in PHP:

$arr[3] = ['key'=>'val', 'key1'=>'val1', 'key2'='val3']

[quote]“Programming is like *ex: one mistake and you’re providing support for a lifetime.”(Michael Sinz)[/quote] [quote]“There are two ways to write error-free programs; only the third one works.”(Alan J. Perlis)[/quote]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Arion,

We have a terminology problem. :(

The array you have posted is perfectly valid, but its parts are described thus:

Index   Element

0       0
1       "Registry"

You can have strings as elements of arrays, but you need a numeric index to identfy the element - I trust that clears up your misconception. :D

Now the Help file does suggest that you should not mix data types within an array - but I have never found that to be a problem, at least not with strings of shortish length such as the one you are using. :D

Your other array would have to be written like this:

$arr[3] = ["key'=>'val", "key1'=>'val1", "key2'='val3"]

; or

$arr[3] = ['key"=>"val', 'key1"=>"val1', 'key2"="val3']

You need to use the "other" quote internally if you use one type to contain the string.

I am afraid I do use PHP so I cannot comment on whether that is similar.

Does that help? :graduated:

M23

Edit: Clarity - I hope! :D

Further edit: "Should not" rather than "cannot" - see below.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Associative array. Search now.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

I agree with trancexx, sounds like you're talking about "Dictionary Object"/"Associative Array", something AutoIt doesn't do natively. Do a search with Titles Only set in the examples forum and you'll find some.

Examples:

I want to be a link too

Share this post


Link to post
Share on other sites

It wants to be a link... :graduated:

Twice I clicked.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Now the Help file does suggest that you cannot mix data types within an array :graduated: where?

Suggests against it, yes. Cannot, never seen that one.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

MvGulik,

Earlier post amended.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

I have often thought of adding bits of code to AutoIt3Wrapper, as an additional pre process part. This was one of the things I thought of.

Input:

$myVar['string']

Output:

$myVar[__getIndex('myVar', 'string')]

It then adds code to the script... Or it could use the dictionary object. It would effectively add dictionary functionality to AutoIt.

If you are only using one or maybe two, and you know their names then another solution is to have a function instead (in a similar way to how you would define a method for C# classes for using indexes):

myVar('string')

Func myVar($sString)
    Switch $sString
        Case ...
    EndSwtich
EndFunc

But that would mean "myVar('string') = 'test'" wouldn't work.

And finally, there is the method I tend to use. Define indexes for a known dictionary as other variables:

Global Const $x = 0
Global Const $y = 1

Local $aPos = MouseGetPos()

MsgBox(0, "Test", "X: " & $aPos[$x] & @CRLF & "Y: " & $aPos[$y])

As you can see, it makes it very easy to see whats going on, and the great news is that it is so easy to do a find and replace at the end to get just the indexes, so you are not using any more memory... I've used that a lot, especially when making big programs with lots of global arrays storing stuff.

Associative arrays as php knows them are not in AutoIt natively though. Maybe one day.

Mat

Share this post


Link to post
Share on other sites

This is piece of cake for AutoItObject. For example:

#include "AutoItObject.au3"
; Initialize
_AutoItObject_Startup()


; Create AutoIt-object
Global $oArray = _AutoItObject_Create()

; Add properties
For $i = 1 To 100
    _AutoItObject_AddProperty($oArray, "key" & $i, $ELSCOPE_PUBLIC, "val" & $i)
Next
; More
_AutoItObject_AddProperty($oArray, "num", $ELSCOPE_PUBLIC, 123.456)
_AutoItObject_AddProperty($oArray, "int", $ELSCOPE_PUBLIC, 7890)

ConsoleWrite("key23 = " & $oArray.key23 & @CRLF)
ConsoleWrite("num = " & $oArray.num & @CRLF)
ConsoleWrite("int = " & $oArray.int & @CRLF)
ConsoleWrite("key87 = " & $oArray.key87 & @CRLF)

; Set new val to key87
$oArray.key87 = "Nev value of key87"

ConsoleWrite("key87 = " & $oArray.key87 & @CRLF)

; etc...

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

These are all good and well, but it merely skirts the fact that AutoIt is missing a very important piece of functionality, one which, in my opinion, should be fixed immediately. Until then though, I guess a workaround of a workaround will suffice. (And yes I was describing associative arrays, it's just been so long since I used PHP that I actually forgot what they were called>)


[quote]“Programming is like *ex: one mistake and you’re providing support for a lifetime.”(Michael Sinz)[/quote] [quote]“There are two ways to write error-free programs; only the third one works.”(Alan J. Perlis)[/quote]

Share this post


Link to post
Share on other sites

I don't like the use of the word "fixed". It's not a bug, or a problem with the language, it's something that's not implemented.

Is there a particular reason you need associative arrays and none of the workarounds work? This is something that isn't really necessary in my opinion. php is something different, as it manages large webpages, often with different scripts doing different jobs that need to store globals etc.

Mat

Share this post


Link to post
Share on other sites

Or the word "immediately" for that matter. :graduated:


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

I always thought associative arrays were just the same as normal arrays, with the addition of an alternative (string) index. :graduated:

Edited by czardas

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