Sign in to follow this  
Followers 0
honk

ArrayBinarySearch

3 posts in this topic

Hi There :whistle:

Am trying to use the ArrayBinarySearch function. It is working well when the data is manually entered like in the example but I would like to read in a data file into the array then search. I have managed to get the data into the array using the _FileReadToArray, then did the sort but whenever I try to match the criteria it never works and keeps showing the Error 3 message :D

Is there someting I need to do when reading a file in?

PS. I have tried converting the array to a string then used the Strintrng function and this is fine but this looks for any instances.

Here is the mods, the text file is just the example names.

Thank you :P

#include <Array.au3>

#include <file.au3>

Dim $avArray[100],$filepath

$filepath = "C:\CATIDUN\1.txt"

_FileReadToArray($filepath,$avArray)

_ArrayDisplay( $avArray, "unsorted" )

;~ $avArray[0] = "JPM"

;~ $avArray[1] = "Holger"

;~ $avArray[2] = "Jon"

;~ $avArray[3] = "Larry"

;~ $avArray[4] = "Jeremy"

;~ $avArray[5] = "Valik"

;~ $avArray[6] = "Cyberslug"

;~ $avArray[7] = "Nutster"

;~ $avArray[8] = "JdeB"

;~ $avArray[9] = "Tylo"

; sort the array to be able to do a binary search

_ArraySort( $avArray)

; display sorted array

_ArrayDisplay( $avArray, "sorted" )

; Lookup existing entry

$iKeyIndex = _ArrayBinarySearch($avArray,"JPM")

If Not @error Then

Msgbox(0,'Entry found',' Index:' & $iKeyIndex)

Else

Msgbox(0,'Entry Not found',' Error:' & @error)

EndIf

;~ ; Lookup None existing entry

;~ $iKeyIndex = _ArrayBinarySearch ( $avArray, "Unknown" )

;~ If Not @error Then

;~ Msgbox(0,'Entry found',' Index:' & $iKeyIndex)

;~ Else

;~ Msgbox(0,'Entry Not found',' Error:' & @error)

;~ EndIf

;~ ;

;~ ; Example 2, using an Array returned by StringSplit

;~ ;

;~ $avArray = StringSplit("a,b,d,c,e,f,g,h,i",",")

;~ ; sort the array to be able to do a binary search

;~ _ArraySort( $avArray, 0, 1)

;~ ; display sorted array

;~ _ArrayDisplay( $avArray, "sorted" )

;~ ; added 1 as second parameter to skip looking in $avArray[0]

;~ $iKeyIndex = _ArrayBinarySearch ( $avArray, "c",1 )

;~ If Not @error Then

;~ Msgbox(0,'Entry found',' Index:' & $iKeyIndex)

;~ Else

;~ Msgbox(0,'Entry Not found',' Error:' & @error)

;~ EndIf

Exit

Share this post


Link to post
Share on other sites



Yep.

I was having the same problem.

I believe it is because the array that is returned from '_FileReadToArray' has an integer in the first position of the array.

It must assume that the data inside the array is the type of element zero.

This smells like a bug, but i'm no AutoIt expert.

The following Displays 'Good'.

Dim $tableArray[4]
$tableArray[0] = "a"
$tableArray[1] = "b"
$tableArray[2] = "my testing"
$tableArray[3] = "c"
$var = "my testing"
_ArraySort( $tableArray)
; Lookup existing entry
$isActiveTable = _ArrayBinarySearch ( $tableArray, $var )
If Not @error Then
    MsgBox( 0, "", "Good" )
Else
    MsgBox( 0, "", "Bad" )
EndIf

The following Displays 'Bad'.

Dim $tableArray[4]
$tableArray[0] = 44
$tableArray[1] = "b"
$tableArray[2] = "my testing"
$tableArray[3] = "c"
$var = "my testing"
_ArraySort( $tableArray)
; Lookup existing entry
$isActiveTable = _ArrayBinarySearch ( $tableArray, $var )
If Not @error Then
    MsgBox( 0, "", "Good" )
Else
    MsgBox( 0, "", "Bad" )
EndIf

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

All you need tro do is change the "base" parameter to 1, for both _ArraySort() and Search, since the 0 entry in the Array contain the number of entries in the Array.

_FileReadToArray($filepath, $avArray)
_ArrayDisplay($avArray, "unsorted")
_ArraySort($avArray,0,1)
; display sorted array
_ArrayDisplay($avArray, "sorted")
; Lookup existing entry
$iKeyIndex = _ArrayBinarySearch($avArray, "JPM",1)
If Not @error Then
    MsgBox(0, 'Entry found', ' Index:' & $iKeyIndex)
Else
    MsgBox(0, 'Entry Not found', ' Error:' & @error)
EndIf
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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