Sign in to follow this  
Followers 0
aslani

Checks if Array Element Exists

7 posts in this topic

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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


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

Sorry, tyvm for answering reguardless... ill try not to do it agian, i didnt even look at the date prior... again sorry and thank you...

Share this post


Link to post
Share on other sites

Ober,

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

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

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