FuryCell Posted June 8, 2005 Posted June 8, 2005 (edited) 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. expandcollapse popup;=============================================================================== ; ; 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;==>_ArraySearchP.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 December 11, 2006 by SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
therks Posted June 8, 2005 Posted June 8, 2005 (edited) 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 June 8, 2005 by Saunders My AutoIt Stuff | My Github
blindwig Posted June 8, 2005 Posted June 8, 2005 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:CODEFunc _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 -1EndFunc ;==>_ArraySearchYou might also want to consider a case insensative mode. My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions
FuryCell Posted June 8, 2005 Author Posted June 8, 2005 (edited) 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 December 11, 2006 by SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
busysignal Posted June 9, 2005 Posted June 9, 2005 SolidSnake, simple and clean array search routine. This will come in handy in a project that I am working on. Thanks..
FuryCell Posted June 9, 2005 Author Posted June 9, 2005 SolidSnake, simple and clean array search routine. This will come in handy in a project that I am working on. Thanks..<{POST_SNAPBACK}>Your WelcomeP.S. Thanks for the comment. HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now