Jump to content

Compare 2 1D arrays (with a same size) quickly


ashly
 Share

Recommended Posts

It depends on the result you need. If you just want to know if they are equal then you could use function _ArrayToString and compare the resulting strings.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You must compare each element from each array otherwise you can't be sure whether both are equal or not!

If the array is sorted than it is much easier to compare, otherwise you need to sort both arrays and compare it afterwards.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

What is "compare" ?

Take

A    A
B    C
C    B

They are DIFFERENT because the order is not the same.

They are IDENTICAL because the content is the same

my ideas would be

case 1) make a string and compare it

case 2) sort and compare - OR add together values of ascii codes and compare total number. (like: 2+3 = 3+2 = 5)

I am just a hobby programmer, and nothing great to publish right now.

Link to comment
Share on other sites

I wrote you a small function.

Global $array1[3] = [1,2,3], $array2[3] = [1,3,2]

If _ArrayCompare($array1, $array2) Then
    MsgBox(0, "", "Arrays are identical")
Else
    MsgBox(0, "", "Arrays are different")
EndIf

Func _ArrayCompare($aArray1, $aArray2)
    If Ubound($aArray1, 0) <> 1 Or Ubound($aArray2, 0) <> 1 Or Ubound($aArray1, 1) <> Ubound($aArray2, 1) Then Return SetError (1, 0, False)

    Local $iBound = UBound($aArray1)
    For $i = 0 To $iBound -1
        If Not ($aArray1[$i] == $aArray2[$i]) Then Return SetError(2, 0, False) ; No more comparisons needed.
    Next
    Return True
EndFunc

I had a small oversight. - Fixed.

Edited by czardas
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...