Jump to content
Sign in to follow this  
FuryCell

UDF : _ArraySearch

Recommended Posts

I wrote this simple UDF becuase i needed to search an array but in order to use _ArrayBinarySearch() the array would have to be sorted.However if I sorted my array it would mess up my program becuase the order was important.

Hope someone else also finds it useful. :(

;===============================================================================
;
; Description:    Finds an entry within an one-dimensional array. (Similar to _ArrayBinarySearch() except the array does not need to be sorted.)
; Syntax:          _ArraySearch ($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0)

; Parameter(s):     $avArray           = The array to search
;                   $vWhat2Find     = What to search $avArray for
;                   $iStart (Optional) = Start array index for search, normally set to 0 or 1. If omitted it is set to 0
;                   $iEnd  (Optional)  = End array index for search. If omitted it is set to Ubound($AvArray)-1
; Requirement(s):   None
; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path
;                   On Failure - Returns an empty string 0 if $vWhat2Find is not found
;                       @Error=1 $avArray is not an array
;                       @Error=2 $iStart is greater than UBound($AvArray)-1
;                       @Error=3 $iEnd is greater than UBound($AvArray)-1
;                       @Error=4 $iStart is greater than $iEnd
; Author(s):        SolidSnake <MetalGX91 at GMail dot com>
; Note(s):          This might be a bit slower than _ArrayBinarySearch() but is useful when the array's order can't be altered.
;===============================================================================
Func _ArraySearch($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0)
    Local $iCurrentPos, $iUBound
    If Not IsArray($avArray) Then
        SetError(1)
        Return 0
    EndIf
    $iUBound = UBound($avArray) - 1
    If $iEnd = 0 Then $iEnd = $iUBound
    If $iStart > $iUBound Then
        SetError(2)
        Return (0)
    EndIf
    If $iEnd > $iUBound Then
        SetError(3)
        Return 0
    EndIf
    If $iStart > $iEnd Then
        SetError(4)
        Return 0
    EndIf
    For $iCurrentPos = $iStart To $iEnd
        If $avArray[$iCurrentPos] == $vWhat2Find Then
            SetError(0)
            Return $iCurrentPos
        EndIf
    Next
    SetError(0)

[COLOR=red]Edit:See post below for updated version. 
    Return 0
EndFunc;==>_ArraySearch

P.S the code header looks weird becuase of the word wrap in the code box. Don't know how to fix it :(

Edit:This UDF is now a standard include as of v3.2 :

Edit:Changed header to reflect my new email address.

Edited by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

Heh, I see two little problems.

First in your header, you have this line:

; Return Value(s):  On Success - Returns an array containing the list of files and folders in the specified path

Files and folders eh? :(

Second, the return shouldn't be 0 if it doesnt' find anything. Because if you search the following array for "Hello" what's it going to return?

Dim $array[2]
$array[0] = "Hello"
$array[1] = "World"

The array search UDF I made for myself returns -1 if it doesn't find anything, since you can't (in AutoIt anyway) have an array index less than 0.

Otherwise, good job. I never used that binary search one. Same as you, I can't often sort the array I'm using before looking for something in it. It has to be in a specific order.

Edited by Saunders

Share this post


Link to post
Share on other sites

P.S the code header looks weird becuase of the word wrap in the code box. Don't know how to fix it  :(

<{POST_SNAPBACK}>

use codebox instead of code, that will put scrollies on it :(

regarding your function, good work! However, and this is just a personal preference, I like to make my functions a little more idiot-proof instead of just erroring out:

CODE

Func _ArraySearch($avArray, $vWhat2Find, $iStart = -1, $iEnd = -1)

Local $iCurrentPos, $iUBound

If Not IsArray($avArray) Then

SetError(1)

Return -1

EndIf

SetError(0)

$iUBound = UBound($avArray) - 1

If $iStart < 0 Then $iStart = 0

If $iEnd < 0 Then $iEnd = $iUBound

If $iStart > $iUBound Then $iStart = $iUBound

If $iEnd > $iUBound Then $iEnd = $iUBound

If $iStart > $iEnd Then $iEnd = $iStart

For $iCurrentPos = $iStart To $iEnd

If $avArray[$iCurrentPos] == $vWhat2Find Then

Return $iCurrentPos

EndIf

Next

Return -1

EndFunc ;==>_ArraySearch

You might also want to consider a case insensative mode.

Share this post


Link to post
Share on other sites

Here is the updated version.

added Blindwigs idea of a Case sense and fixed the bug with the return that Saunders notified me of.

Thanks Blindwig and Saunders.

CODE
;===============================================================================

;

; Description: Finds an entry within an one-dimensional array. (Similar to _ArrayBinarySearch() except the array does not need to be sorted.)

; Syntax: _ArraySearch($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0,$iCaseSense=0)

;

; Parameter(s): $avArray = The array to search

; $vWhat2Find = What to search $avArray for

; $iStart (Optional) = Start array index for search, normally set to 0 or 1. If omitted it is set to 0

; $iEnd (Optional) = End array index for search. If omitted or set to 0 it is set to Ubound($AvArray)-

; $iCaseSense (Optional) = if set to 1 then search is case sensitive

; Requirement(s): None

; Return Value(s): On Success - Returns the position of an item in an array.

; On Failure - Returns an empty string "" if $vWhat2Find is not found

; @Error=1 $avArray is not an array

; @Error=2 $iStart is greater than UBound($AvArray)-1

; @Error=3 $iEnd is greater than UBound($AvArray)-1

; @Error=4 $iStart is greater than $iEnd

; @Error=5 $iCaseSense was invalid. (Must be 0 or 1)

; Author(s): SolidSnake <MetalGX91 at GMail dot com>

; Note(s): This might be a bit slower than _ArrayBinarySearch() but is useful when the array's order can't be altered.

;===============================================================================

Edit:Changed header file to reflect my new email address. (Previous downloads 89)

Edited by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

SolidSnake, simple and clean array search routine. This will come in handy in a project that I am working on.

Thanks..

<{POST_SNAPBACK}>

Your Welcome

P.S. Thanks for the comment. :(


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...