Jump to content

Recommended Posts

Posted

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

Posted

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.

  • 4 years later...
Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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