Sign in to follow this  
Followers 0
ashly

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

5 posts in this topic

i would like to ask if there's any way to compare 2 1D arrays quickly rather than using for-next loop to compare them one by one?

thanks.

Share this post


Link to post
Share on other sites



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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

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  
Followers 0