Sign in to follow this  
Followers 0
botanic

access array index by name

9 posts in this topic

is there any way i can access an array via its index name rather then a number

I have the following array and

$usersettings[1][1]

works however

$usersettings['vss'][1]

does not

 

[0]|10|
[1]|vss|4
[2]|username|
[3]|password|
[4]|compress|6
[5]|limitbw|4
[6]|bwlimit|
[7]|index|4
[8]|backuplength|30
[9]|systemstate|4
[10]|systemstatepath|

 

Share this post


Link to post
Share on other sites



_ArraySearch() will give you the index of the element where a string is found. here's a small example:

#include <Array.au3>

Dim $a[4][2]

$a[3][0]='vss'
$a[3][1]=4
; the rest of the array is empty

MsgBox(0,'',$a[_ArraySearch($a,'vss')][1])

Share this post


Link to post
Share on other sites

botanic,

When I want to refer to an array index by some name instead of offset I do it like this...

#include <array.au3>

local enum $vss = 1, $username, $password, $compress, $limitbw, $bwlimit, $index, $backuplength, $systemstate, $systemstatepath

local $array[10] = [10]

$array[$username] = 'some user name'
$array[$password] = 'some password'
$array[$systemstate] = 'FUBAR'

_arraydisplay($array)

The drawback to this technique is that the ENUM defs have to be kept in sync with the array (if you expand or contract the array at some point).

I've never used the UDF pointed to by hannes08.  It may eliminate this problem.

One other alternative is to use the dictionary object.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

kylomas,

One way of keeping the Enums and the array in sync is to do this (taken from one of my scripts):

Local Enum $eStart, $eReset, $eRepeat, $eReload, $eFile, $eRecent, $eZone, $eRepeat_Option_State, $eZone_Option_State, _
        $eFolder_Name, $eFolder_Alias, $eFolder_Data_File, $eMax
Local $aParam_List[$eMax]
Now the array will always have the correct number of elements - and if you want a count in [0] it is easy to arrange. :)

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

@M23 - Thanks...


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

* all

and you do not know what in the beta already have unofficial support for associative arrays?

Local $table[]
$table['vss'] = 'hello)'
ConsoleWrite($table['vss'] & @LF)

Share this post


Link to post
Share on other sites

Inververs,

That functionality is not complete or stable, will certainly not be supported by any of the AutoIt team and is completely undocumented as to its proper use - plus it could be removed at any time. I strongly suggest that people do NOT start using it until such time as it is formally announced as at least an "Experimental" item - perhaps in an eventual 3.3.11.## Beta. ;)

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

well, let's hope that this feature will make part Autoit.

tnx :)

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