Jump to content

Array Search Failing To Find Match


BatMan22
 Share

Recommended Posts

I wrote a function to find any duplicate dates in a excel column by using ArraySearch but ArraySearch doesn't yield anything even when I paste in duplicate data.. I can SEE the duplicate data in the _ArrayDisplay function... What am I doing wrong? Eventually I just want this function to delete any column with duplicate dates (dates that have already been seen). Is there an easier way to do this?

 

Func CheckForDups()
    $dateandtimerow = "2"
    $LastCol = "L"
    local $rangetoread = "B" & $dateandtimerow & ":" & $lastcol & $dateandtimerow
    ConsoleWrite("TheRangeWereLooking at dates and times are: " & $rangetoread & @CRLF)
    local $DATARRAY = _Excel_RangeRead($oname, "Edit Here", $rangetoread)
    _ArrayDisplay($DATARRAY)
    For $i = 0 to UBound($DATARRAY, 2)-1
        local $found = _ArraySearch($DATARRAY, $DATARRAY[0][$i])
        ConsoleWrite("Found at : " & $found & @CRLF)
    Next
    Exit
EndFunc

 

DuplicateDates.xlsx

Link to comment
Share on other sites

It is finding matches, all of your data is in one row, _ArraySearch tells you what row your match was found in, not the subitem of that row.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You can search the row, see $bRow parameter, however it would be better to use _ArrayFindAll as this will find all instances, example:

#include <Array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\DuplicateDates.xlsx")

CheckForDups()

Func CheckForDups()
    Local $aSearch
    $dateandtimerow = "2"
    $LastCol = "L"
    local $rangetoread = "B" & $dateandtimerow & ":" & $lastcol & $dateandtimerow
    ConsoleWrite("TheRangeWereLooking at dates and times are: " & $rangetoread & @CRLF)
    local $aDataArray = _Excel_RangeRead($oWorkbook, "Edit Here", $rangetoread)
    _ArrayDisplay($aDataArray)
    For $i = UBound($aDataArray, 2) - 1 To 0 Step - 1
        $aSearch = _ArrayFindAll($aDataArray, $aDataArray[0][$i], 0, 0, 0, 0, 0, True)
        If UBound($aSearch) - 1 = 0 Then ContinueLoop
        For $j = UBound($aSearch) - 1 To 1 Step - 1
            _ArrayColDelete($aDataArray, $aSearch[$j])
            $i -= 1
        Next
    Next
    _ArrayDisplay($aDataArray)
EndFunc

 

Link to comment
Share on other sites

@Subz, if you don't mind me asking a part two to my question.. I tried to use/modify what you wrote to delete the column in the excel with the duplicate date in it.. and it deletes a lot more then just those rows... I switched back to ArraySearch from the ArrayFindAll because I couldn't figure out how to use the output from the FindAll to do what I needed (delete one of the duplicate rows). 

 

For $i = UBound($aDateArray, 2) - 1 To 0 Step -1
        $aSearch = _ArraySearch($aDateArray, $aDateArray[0][$i], 0, Default, Default, Default, Default, Default, True)
        If $aSearch <> $aDateArray[0][$i] Then ; If it finds anything BUT itself then delete THAT row.. Problem seems to originating here..
            _Excel_RangeDelete($oname.ActiveSheet, _Excel_ColumnToLetter($aSearch) & ":" & _Excel_ColumnToLetter($aSearch) , Default, 2)
        EndIf
        ConsoleWrite("FOUND IS: " & $aSearch & @CRLF)
    Next
    Exit

 

Link to comment
Share on other sites

Here is a working example for both _ArraySearch and _ArrayFindAll, just uncomment/comment one or the other, I find that you need to keep your array in sync with your spreadsheet when searching, _ArraySearch will search one by one, whereas _ArrayFindAll will find all instances and delete all but the first item found.  Hope that makes sense.

#include <Array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\DuplicateDates.xlsx")

CheckForDups()

Func CheckForDups()
    Local $aSearch, $iFirstColumn
    Local $iDateandTimeRow = "2"
    Local $sFirstColumn = "B"
    Local $iFirstColumn = _Excel_ColumnToNumber($sFirstColumn)
    Local $sLastColumn = "L"
    local $rangetoread = $sFirstColumn & $iDateandTimeRow & ":" & $sLastColumn & $iDateandTimeRow
    ConsoleWrite("TheRangeWereLooking at dates and times are: " & $rangetoread & @CRLF)
    local $aDateArray = _Excel_RangeRead($oWorkbook, "Edit Here", $rangetoread)
    _ArrayDisplay($aDateArray)
    For $i = 0 To UBound($aDateArray, 2) - 1
        $iSearch = _ArraySearch($aDateArray, $aDateArray[0][$i], $i + 1, 0, 0, 0, 1, -1, True)
        If $iSearch = -1 Then ContinueLoop
        ConsoleWrite($aDateArray[0][$i] & " - " & $iSearch & " : " & _Excel_ColumnToLetter($iSearch + $iFirstColumn) & ":" & _Excel_ColumnToLetter($iSearch + $iFirstColumn) & @CRLF)
        _Excel_RangeDelete($oWorkbook.ActiveSheet, _Excel_ColumnToLetter($iSearch + $iFirstColumn) & ":" & _Excel_ColumnToLetter($iSearch + $iFirstColumn), Default, 2)
        _ArrayColDelete($aDateArray, $iSearch)
        $i -= 1
    Next

;~     For $i = UBound($aDateArray, 2) - 1 To 0 Step - 1
;~         $aSearch = _ArrayFindAll($aDateArray, $aDateArray[0][$i], 0, 0, 0, 0, 0, True)
;~         If UBound($aSearch) - 1 = 0 Then ContinueLoop
;~         For $j = UBound($aSearch) - 1 To 1 Step - 1
;~             _ArrayColDelete($aDateArray, $aSearch[$j])
;~          $iFirstColumn = _Excel_ColumnToNumber($sFirstColumn)
;~          _Excel_RangeDelete($oWorkbook.ActiveSheet, _Excel_ColumnToLetter($aSearch[$j] + $iFirstColumn) & ":" & _Excel_ColumnToLetter($aSearch[$j] + $iFirstColumn), Default, 2)
;~             $i -= 1
;~         Next
;~     Next
    _ArrayDisplay($aDateArray)
EndFunc

 

Link to comment
Share on other sites

13 hours ago, Subz said:

For $j = UBound($aSearch) - 1 To 1 Step - 1

THAT'S what I didn't understand/didn't know how to deal with.. With that line you're skipping the matching of itself right? That's why it's Ubound-1 to 1 instead of Ubound-1 to 0?

Link to comment
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
 Share

  • Recently Browsing   0 members

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