Sign in to follow this  
Followers 0
dcat127

Compare 2 multidimensional arrays

3 posts in this topic

#1 ·  Posted (edited)

How do you compare 2 arrays? the = operator does not work i.e.

Dim $array[2][2], $array2[2][2]
$array[0][0] = "word1"
$array[0][1] = "word2"
$array[1][0] = "word1"
$array[1][1] = "word2"
if $array = $array2 Then  ;this line does not work
    MsgBox(0,"Match","")
EndIf
$array[1][1] = "different word"
if $array = $array2 Then  ;this line does not work
    MsgBox(0,"Match","")
EndIf
Edited by dcat127

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Dim $array[2][2], $array2[2][2]
$array[0][0] = "word1"
$array[0][1] = "word2"
$array[1][0] = "word3"
$array[1][1] = "word4"
                                          ;;change one of these items to make the msgbox reflect NO MATCH
$array2[0][0] = "word1"
$array2[0][1] = "word2"
$array2[1][0] = "word3"
$array2[1][1] = "word4"

$size = ubound($Array) - 1
For $i = 0 to $size 
if $array[$i][0] = $array2[$i][0] AND $array[$i][1] = $array2[$i][1] Then 
    MsgBox(0,"Match","Match")
Else
    MsgBox(0,"NO Match","NO Match")
    EndIf
next

exit

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Func _Compare_2D_Arrays(ByRef $first_array, ByRef $second_array)    ; main function!

    If UBound($first_array,0) <> UBound($second_array,0) _

    And UBound($first_array) <> 2 _

    And UBound($first_array,1) <> UBound($second_array,1) _

    And UBound($first_array,2) <> UBound($second_array,2) _

    Then Return SetError(1, 0, 0)   ; return 0, so they are NOT the same, and set error, couse they're not even close to "same",

                                    ; because of different number of elements and/or dimensions

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

        For $j = 0 To UBound($first_array) -1 ; since they've same length, no matter witch UBound we will use

            If $first_array[$i][$j] <> $second_array[$i][$j] Then Return 0 ; they are NOT the same

        Next

    Next



    Return 1 ;they ARE the same

EndFunc



; creating arrays to compare

Global $array1[15][15]

Global $array2[15][15]



For $i = 0 To 14    ; filling the arrays with different numbers

    For $j = 0 to 14

        $array1[$i][$j] = Random(1,100,1)

        $array2[$i][$j] = Random(1,25)

    Next

Next



 ; so they wont be the same



If _Compare_2D_Arrays($array1, $array2) Then   ;comparing! 

    MsgBox(0,"","same")

Else

    MsgBox(0,"","different")

EndIf





For $i = 0 To 14    ; filling the arrays with SAME numbers

    For $j = 0 to 14

        $random = Random(1,100,1)

        $array1[$i][$j] = $random

        $array2[$i][$j] = $random

    Next

Next



; so they WILL be the same



If _Compare_2D_Arrays($array1, $array2) Then   ;comparing! 

    MsgBox(0,"","same")

Else

    MsgBox(0,"","different")

EndIf

Edited by 4ggr35510n

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