ashly Posted November 29, 2012 Posted November 29, 2012 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.
water Posted November 29, 2012 Posted November 29, 2012 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 2024-07-28 - Version 1.6.3.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 (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
UEZ Posted November 29, 2012 Posted November 29, 2012 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Myicq Posted November 29, 2012 Posted November 29, 2012 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.
czardas Posted November 29, 2012 Posted November 29, 2012 (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 November 29, 2012 by czardas operator64 ArrayWorkshop
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now