Jump to content

Checks if Array Element Exists


aslani
 Share

Recommended Posts

Is there a function to check if an element exists in an array?

Example:

$avArray[0] = Start
$avArray[1] = Beginning
...
$avArray[10] = End

$parArray = _ArrayCreate($avArray[2])

;This one works since $avArray[6] exists, but...
If $avArray[6] Then
_ArrayAdd($parArray, $avArray[6])
EndIf

; ...if the element doesn't exist, it exits on error rather than simply doing nothing
If $avArray[11] Then
_ArrayAdd($parArray, $avArray[11])
EndIf

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

Introduce a check for UBound($myArray). If the element number you want to access is larger than UBound($myArray)-1, it doesn't exist in the array. (FYI: the -1 because UBound always returns one more than the last element number since arrays are 0-based and the [0]'th element also counts.)

Something like:

If _elementExists($avArray, 11) Then
    _ArrayAdd($parArray, $avArray[11])
EndIf

Func _elementExists($array, $element)
    If $element > UBound($array)-1 Then Return False ; element is out of the array bounds
    Return True ; element is in array bounds
EndFunc

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

  • 4 years later...

How about finding the item inside the array... IE Start, Begining and End -- is there a way to do an "exists" on an array to see if any one of those is in the array or not? in those cases they would all come up true but if you looked for banana you would get false?

thanks...

Link to comment
Share on other sites

  • Moderators

Ober,

Please do not necro-post in topics generally (did you notice the post above yours was made nearly 5 years ago? ;)), and certainly not in ones that have nothing to do with your question. :D

As to the question itself - if you want to test for the existence of a certain element within an array, then use _ArraySearch. :)

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

Ober,

No problem - just look more closely next time. ;)

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