Jump to content
Sign in to follow this  
erezlevi

how to compare two Arrays?

Recommended Posts

erezlevi

hi,

how can I compare the two arrays and return where the compare failed within it?

for example:

#include <Array.au3>

Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
dim $array2[5]=["Erez","Do","Know","To","Compare"]

_ArrayDisplay ($array1)
_ArrayDisplay ($array2)

I need to get the Array position within the two where the "Don't" and "Do" does not match.

Share this post


Link to post
Share on other sites
Siao

Ever heard of loops?


"be smart, drink your wine"

Share this post


Link to post
Share on other sites
erezlevi

Ever heard of loops?

got it working thanks,

#include <Array.au3>

Dim $array1[6]=["Erez","Don't","Know","To","Compare"]
dim $array2[6]=["Erez","Do","Know","Tu","Compare"]
dim $array3[1]
_ArrayDisplay ($array1)
_ArrayDisplay ($array2)
for $o=1 to 5
$m=StringCompare ($array1[$o],$array2[$o],1)
If $m <> 0 Then
    _ArrayAdd ($array3,$o)
    
EndIf
Next
;MsgBox (0,"this is where it happened within the string",
    
_ArrayDisplay ($array3)

Share this post


Link to post
Share on other sites
weaponx

This is a start:

;Check for different elements (returns 1)
Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
Dim $array2[5]=["Erez","Do","Know","To","Compare"]

$result = _ArrayCompare($array1, $array2)
MsgBox(0,"",$result)


;Check for mismatched number of elements (returns 4)
Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
Dim $array2[5]=["Erez","Don't","Know","To"]

$result = _ArrayCompare($array1, $array2)
MsgBox(0,"",$result)

;Check for mismatched number of elements (returns -1)
Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
Dim $array2[5]=["Erez","Don't","Know","To","Compare"]

$result = _ArrayCompare($array1, $array2)
MsgBox(0,"",$result)

Func _ArrayCompare(ByRef $ACarray1, ByRef $ACarray2)
    $numElements1 = Ubound($ACarray1)
    $numElements2 = Ubound($ACarray2)
    For $X = 0 to $numElements1 - 1
        If $X > $numElements2 -1 OR $ACarray1[$X] <> $ACarray2[$X] Then Return $X
        Next
        Return -1
EndFunc
Edited by weaponx

Share this post


Link to post
Share on other sites
randallc

Hi,

This is some more!..

; ArrayCompareEx.au3
#include-once
#include <Array.au3>
#include <ArrayDupes3.au3>

Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
dim $array2[5]=["Erez","Do","Know","To","Compare"]

;ArrayCompare(ByRef $arrItems, ByRef $arrItems2, $iDetails = 0, $iType = 0, $istarti = 0, $istartj = 0)  ;$iType=1 returns the Dupes instead of non-matching lines
local $arAnsw=_ArrayCompare( $array1,  $array2,1,1)
_ArrayDisplay ($arAnsw,"Dupes",0,0,chr(1))
local $arAnsw=_ArrayCompare( $array1,  $array2,1,0)
_ArrayDisplay ($arAnsw,"Extra",0,0,chr(1))
Dim $array1[5]=["Erez","Know","Don't","To","Compare"]
dim $array2[5]=["Erez","Do","Know","Tooo","Compare"]

; ;ArrayCompare(ByRef $arrItems, ByRef $arrItems2, $iDetails = 0, $iType = 0, $istarti = 0, $istartj = 0)  ;$iType=1 returns the Dupes instead of non-matching lines
local $arAnsw=_ArrayCompare( $array1,  $array2,1,1)
_ArrayDisplay ($arAnsw,"Dupes matched",0,0,chr(1))
local $arAnsw=_ArrayCompare( $array1,  $array2,1,0)
_ArrayDisplay ($arAnsw,"Non-matched",0,0,chr(1))
local $arAnsw=_ArrayCompare( $array1,  $array2,0,0)
_ArrayDisplay ($arAnsw,"Non-matched, no details",0,0,chr(1))


;Check for mismatched number of elements (returns 4)
Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
Dim $array2[5]=["Erez","Don't","Know","To"]

local $arAnsw=_ArrayCompare( $array1,  $array2,0,0)
_ArrayDisplay ($arAnsw,"Non-matched, details",1,0,chr(1))
local $arAnsw=_ArrayCompare( $array1,  $array2,1,1)
_ArrayDisplay ($arAnsw,"Dupes matched details",1,0,chr(1))

;Check for mismatched number of elements (returns -1)
Dim $array1[5]=["Erez","Don't","Know","To","Compare"]
Dim $array2[5]=["Erez","Don't","Know","To","Compare"]
local $arAnsw=_ArrayCompare( $array1,  $array2,1,1)
_ArrayDisplay ($arAnsw,"Dupes matched details",1,0,chr(1))
local $arAnsw=_ArrayCompare( $array1,  $array2,0,0)
_ArrayDisplay ($arAnsw,"Non-matched, no details",0,0,chr(1))
Best, Randall

Share this post


Link to post
Share on other sites
Automan Empire

Hi,

This is some more!..

Nice work Randall. Just what I was looking for.


"I've seen your work in the past, and it's novice at best..." SmOke_N

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  

×