Jump to content

Recommended Posts

Posted

So I was attempting to work the ideas at this post into my program

http://www.autoitscript.com/forum/index.ph...topic=18819&hl=

But for some reason _ArraySearch is not comparing the items in the arrays correctly. All the program ends up doing is spitting out the first list again. Can anyone figure out what's wrong?? I've included the text files for example. I want the end result to be that if an item in the dailylist file is not found in the craplist file for it to be written to a third file.

Thanks

Func _CompareFiles($filename_1, $filename_2)

  $sFileName_1 = $filename_1
  $sFileName_2 = $filename_2
  $FileName_Diff = @scriptdir & "\Compared_DIFF.txt"

  Dim $File1Array[1200], $File2Array[1200]
  Global $File1Offset = 0, $File2Offset = 0

 ; Create an empty file for writing the result to.
  $File_3 = FileOpen($FileName_Diff, 2)
  If $File_3 = -1 Then
      MsgBox(0, "Error", "Unable to create file : " & $FileName_Diff)
      Exit
  EndIf
  FileClose($File_3)


 ; Open the first file
  $File_1 = FileOpen($sFileName_1, 0)
  If $File_1 = -1 Then
      MsgBox(0, "Error", "Unable to open file : " & $sFileName_1)
      Exit
  EndIf
  $File_2 = FileOpen($sFileName_2, 0)
  If $File_2 = -1 Then
      MsgBox(0, "Error", "Unable to open file : " & $sFileName_2)
      Exit
  EndIf

  _FileReadToArray ($sFileName_1, $File1Array)
  _FileReadToArray ($sFileName_2, $File2Array)


  For $i = 1 To UBound ($File1Array) - 1
      If _ArraySearch($File2Array, $File1Array[$i]) = "" Then
         ;Guictrlsetdata($testlabel,$file1Array[$i]); testing label
          $DidWrite = Write_Diff_Log($FileName_Diff, $File1Array[$i])
          If $DidWrite = 0 Then
              MsgBox (0, "Error", "Could not write to file")
              Exit
          EndIf
      EndIf
  Next


EndFunc

; This function adds a Message in a File
; It returns 0 on failure
; It returns 1 on success
Func Write_Diff_Log($LogFileName, $LogMessage)
    Local $File_3
    Local $WriteToFile_3

    $File_3 = FileOpen($LogFileName, 1)
    If $File_3 = -1 Then
        Return 0
    EndIf
    $WriteToFile_3 = FileWriteLine($File_3, $LogMessage & @CRLF)
    If $WriteToFile_3 = -1 Then
        Return 0
    EndIf
    FileClose($File_3)
    Return 1

EndFunc

;===============================================================================
;
; 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 <MetalGearX91@Hotmail.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, $iCaseSense = 0)
    Local $iCurrentPos, $iUBound
    If Not IsArray($avArray) Then
        SetError(1)
        Return ""
    EndIf
    $iUBound = UBound($avArray) - 1
    If $iEnd = 0 Then $iEnd = $iUBound
    If $iStart > $iUBound Then
        SetError(2)
        Return ""
    EndIf
    If $iEnd > $iUBound Then
        SetError(3)
        Return ""
    EndIf
    If $iStart > $iEnd Then
        SetError(4)
        Return ""
    EndIf
    If Not ($iCaseSense = 0 Or $iCaseSense = 1) Then
        SetError(5)
        Return ""
    EndIf
    For $iCurrentPos = $iStart To $iEnd
        Select
            Case $iCaseSense = 0
                If $avArray[$iCurrentPos] = $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
            Case $iCaseSense = 1
                If $avArray[$iCurrentPos] == $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
        EndSelect
        
    Next
    SetError(0)
    Return ""
EndFunc ;==>_ArraySearch

dailylist.txt

craplist.txt

Posted (edited)

Hi,

does this help?

#include <Array.au3>
#include <File.au3>

_CompareFiles(@ScriptDir & "\day.txt", @ScriptDir & "\file1.txt")

Func _CompareFiles($filename_1, $filename_2)
    Dim $file1_A
    _FileReadToArray($filename_1, $file1_A)
    Dim $file2_A
    _FileReadToArray($filename_2, $file2_A)
    
    For $i = 1 To UBound($file2_A) - 1
        If _ArraySearch($file2_A, $file1_A[$i]) = -1 Then
            MsgBox(0, "Found", $file1_A[$i])
        EndIf
    Next
EndFunc ;==>_CompareFiles

So long,

Mega

P.S.: There was nothing different, so I changed the dayfile and added something. :)

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted

ok...update....there were serious problems with the arraysearch function...now I think I fixed it but it only enters the IF statement once and I can't figure out why. I tried

If $avArray[$iCurrentPos] = $vWhat2Find Then

and then switched it to what it says below in case it was reading the strings differently but it still doesn't work. So, with the same data as posted above, it gets rid of 1 of the matching entried.Any ideas?

Func _ArraySearch($avArray, $vWhat2Find)

Local $iCurrentPos

Local $iUBound

Local $iStart

$iUBound = UBound($avArray) - 1

$iStart = 1

For $iCurrentPos = $iStart To $iUbound

$iCurrentPos = $iCurrentpos +1

If StringinStr($avArray[$iCurrentPos],$vWhat2Find) > 0 Then

Return $iCurrentPos

endif

Next

Return 0

EndFunc ;==>_ArraySearch

Func _CompareFiles($filename_1, $filename_2)

$sFileName_1 = $filename_1

$sFileName_2 = $filename_2

$FileName_Diff = @scriptdir & "\Compared_DIFF.txt"

Dim $File1Array[1200], $File2Array[1200]

Global $File1Offset = 0, $File2Offset = 0

; Create an empty file for writing the result to.

$File_3 = FileOpen($FileName_Diff, 2)

If $File_3 = -1 Then

MsgBox(0, "Error", "Unable to create file : " & $FileName_Diff)

Exit

EndIf

FileClose($File_3)

; Open the first file

$File_1 = FileOpen($sFileName_1, 0)

If $File_1 = -1 Then

MsgBox(0, "Error", "Unable to open file : " & $sFileName_1)

Exit

EndIf

$File_2 = FileOpen($sFileName_2, 0)

If $File_2 = -1 Then

MsgBox(0, "Error", "Unable to open file : " & $sFileName_2)

Exit

EndIf

_FileReadToArray ($sFileName_1, $File1Array)

_FileReadToArray ($sFileName_2, $File2Array)

For $i = 0 To UBound ($File1Array) - 1

$i = $i + 1

If _ArraySearch($File2Array, $File1Array[$i]) = 0 Then

$DidWrite = Write_Diff_Log($FileName_Diff, $File1Array[$i])

EndIf

Next

EndFunc

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
  • Recently Browsing   0 members

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