Jump to content

access array index by name


botanic
 Share

Recommended Posts

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|

 

Link to comment
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])

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...