Jump to content
HHS

ARRAY.AU3 / _ArrayBinarySearch() / Bugfix

Recommended Posts

HHS

Hello,

I have found a "bug" in array.au3 in the function _ArrayBinarySearch().

Summary:

Not possible to get the correct index of an element when use "ß", they found always "ss".

 

Here a little Test-Script:

#include <Array.au3>

Local $Array[6] = ["a_ausser", "a_außer", "b_ausser", "b_außer", "c_ausser", "c_außer"]
_ArraySort($Array, 0, 0)
For $i=0 to UBound($Array)-1
    ConsoleWrite("Index " & $i & ": " & $Array[$i] & @CRLF)
Next

;BinarySearch
ConsoleWrite("BinarySearchIndex: " & _ArrayBinarySearch($Array, "b_außer", 0) & " <-- must be Index 3 - b_außer" & @CRLF)
ConsoleWrite("BinarySearchIndex: " & _ArrayBinarySearch($Array, "b_ausser", 0) & " <-- must be Index 2 - b_ausser" & @CRLF)

Output:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
Index 0: a_ausser
Index 1: a_außer
Index 2: b_ausser
Index 3: b_außer
Index 4: c_ausser
Index 5: c_außer
BinarySearchIndex: 2 <-- must be Index 3 - b_außer
BinarySearchIndex: 2 <-- must be Index 2 - b_ausser
+>09:35:08 AutoIt3.exe ended.rc:0

Here is the wrong item with Index 2 found, but I search for item 3

 

now HERE my little bugfix in __ArrayBinarySearch():

#include <Array.au3>

Local $Array[6] = ["a_ausser", "a_außer", "b_ausser", "b_außer", "c_ausser", "c_außer"]
_ArraySort($Array, 0, 0)
For $i=0 to UBound($Array)-1
    ConsoleWrite("Index " & $i & ": " & $Array[$i] & @CRLF)
Next

;BinarySearch
ConsoleWrite("BinarySearchIndex: " & __ArrayBinarySearch($Array, "b_außer", 0) & " <-- must be Index 3 - b_außer" & @CRLF)
ConsoleWrite("BinarySearchIndex: " & __ArrayBinarySearch($Array, "b_ausser", 0) & " <-- must be Index 2 - b_ausser" & @CRLF)


; #FUNCTION# ====================================================================================================================
; Author ........: Jos
; Modified.......: Ultima - added $iEnd as parameter, code cleanup; Melba23 - added support for empty & 2D arrays
; ===============================================================================================================================
Func __ArrayBinarySearch(Const ByRef $aArray, $vValue, $iStart = 0, $iEnd = 0, $iColumn = 0)
    If $iStart = Default Then $iStart = 0
    If $iEnd = Default Then $iEnd = 0
    If $iColumn = Default Then $iColumn = 0
    If Not IsArray($aArray) Then Return SetError(1, 0, -1)
    ; Bounds checking
    Local $iDim_1 = UBound($aArray, $UBOUND_ROWS)
    If $iDim_1 = 0 Then Return SetError(6, 0, -1)
    If $iEnd < 1 Or $iEnd > $iDim_1 - 1 Then $iEnd = $iDim_1 - 1
    If $iStart < 0 Then $iStart = 0
    If $iStart > $iEnd Then Return SetError(4, 0, -1)
    Local $iMid = Int(($iEnd + $iStart) / 2)
    Switch UBound($aArray, $UBOUND_DIMENSIONS)
        Case 1
            If $aArray[$iStart] > $vValue Or $aArray[$iEnd] < $vValue Then Return SetError(2, 0, -1)
            ; Search
            While $iStart <= $iMid And Not ($vValue == $Array[$iMid])
                If $vValue < $aArray[$iMid] Then
                    $iEnd = $iMid - 1
                Else
                    $iStart = $iMid + 1
                EndIf
                $iMid = Int(($iEnd + $iStart) / 2)
            WEnd
            If $iStart > $iEnd Then Return SetError(3, 0, -1) ; Entry not found
        Case 2
            Local $iDim_2 = UBound($aArray, $UBOUND_COLUMNS) - 1
            If $iColumn < 0 Or $iColumn > $iDim_2 Then Return SetError(7, 0, -1)
            If $aArray[$iStart][$iColumn] > $vValue Or $aArray[$iEnd][$iColumn] < $vValue Then Return SetError(2, 0, -1)
            ; Search
            While $iStart <= $iMid And Not ($vValue == $aArray[$iMid][$iColumn])
                If $vValue < $aArray[$iMid][$iColumn] Then
                    $iEnd = $iMid - 1
                Else
                    $iStart = $iMid + 1
                EndIf
                $iMid = Int(($iEnd + $iStart) / 2)
            WEnd
            If $iStart > $iEnd Then Return SetError(3, 0, -1) ; Entry not found
        Case Else
            Return SetError(5, 0, -1)
    EndSwitch
    Return $iMid
EndFunc   ;==>_ArrayBinarySearch

Output:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
Index 0: a_ausser
Index 1: a_außer
Index 2: b_ausser
Index 3: b_außer
Index 4: c_ausser
Index 5: c_außer
BinarySearchIndex: 3 <-- must be Index 3 - b_außer
BinarySearchIndex: 2 <-- must be Index 2 - b_ausser
+>09:37:17 AutoIt3.exe ended.rc:0

Here is the correct item with Index 3 found.

 

Only two lines have little changes:

line 25 in function:
Original: While $iStart <= $iMid And  $vValue <> $Array[$iMid]
FIX: While $iStart <= $iMid And Not ($vValue == $Array[$iMid])
line 39 in function:
Original: While $iStart <= $iMid And $vValue <> $aArray[$iMid][$iColumn])
FIX: While $iStart <= $iMid And Not ($vValue == $aArray[$iMid][$iColumn])

 

Please check my little bugfix and check in in the repository for the next release.

Edited by HHS
  • Like 1

Share this post


Link to post
Share on other sites
czardas

Thanks for reporting the bug. Unfortunately your fix has an issue: this function is currently not case sensitive and therefore the change would be script breaking. Your suggestion would work with the addition of an extra case sensitivity parameter. There are probably other alternatives, but this seems to be the easiest solution to this problem. It's still not perfect.

Edited by czardas

Share this post


Link to post
Share on other sites
jchd

I believe it all depends on which options are used by the internal call to StringCompareW. Unicode can be misleading, but German usually consider ß to be equivalent to ss.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
       
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

       
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
×