Sign in to follow this  
Followers 0
rudi

$Array1==$Array2, but a compare fails. Why?

5 posts in this topic

Hi.

I tried to directly compare two arrays, holding identical data. This fails. Why?

$ini="C:\my-test.ini"
$section="Global"

Dim $ReferenceArr[8][2] = [[7], _
        ["PortNumber", "12345"], _
        ["ConnectionType", "HTTP"], _
        ["AutoDial", "0"], _
        ["UseVendor", "0"], _
        ["UserPassword", "somethingcryptic"], _
        ["UserName", "someusername"], _
        ["ConnectionAddress", "http://update.customer.de:12345/path/path/"]]
        
for $i=1 to $ReferenceArr[0][0]
    IniWrite($ini,$section,$ReferenceArr[$i][0],$ReferenceArr[$i][1])
Next

; Reading the INI content to a 2nd Array:
$INIArr=IniReadSection($ini,$section)

; compare it to the Reference Array fails:
if $INIArr=$ReferenceArr Then
    MsgBox(0,"Match","Arrays match")
Else
    MsgBox(48,"Mismatch","Arrays do *NOT* match")
EndIf


; Comparing Arrays cell by cell *DOES* match:
if $INIArr[0][0]=$ReferenceArr[0][0] Then
    for $i = 0 to $INIArr[0][0]
        ConsoleWrite($i & "-----------------" & @CRLF)
        ConsoleWrite( $INIArr[$i][0] & ", " & ($INIArr[$i][0]=$ReferenceArr[$i][0]) & @CRLF)
        ConsoleWrite( $INIArr[$i][1] & ", " & ($INIArr[$i][1]=$ReferenceArr[$i][1]) & @CRLF)
    Next
Else
    ConsoleWrite("Arrays don't match for [0][0]" & @CRLF)
EndIf

Confused, I honestly can't see what's my mistake.

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



IniReadSection returns an array, which I guess can only be compared to another array.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Confused, I honestly can't see what's my mistake.

You cannot compare arrays by comparing the Array variablenames.

You will have to compare each entry of the 2 arrays and possible first need to sort them both to ensure they are in the correct sequence.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Try this. It was a quick script, and not tested so be careful ;)

;; Setup the arrays
Dim $arrayOne[4] = ["James", "AutoIt", "Script", "FIRe!"]
Dim $arrayTwo[3] = ["JaMES", "Autoit", "script"]
Dim $arrayThree[4] = ["James", "AutoIt", "Script", "FIRe!"]

;; Compare array one and two
ConsoleWrite("-> Test one" & @CRLF)
If _ArrayCompare($arrayOne, $arrayTwo) Then
    MsgBox(0, "", "$arrayOne and $arrayTwo match!")
Else
    MsgBox(0, "", "$arrayOne and $arrayTwo don't match!")
EndIf

;; Compare array one and three
ConsoleWrite("-> Test two" & @CRLF)
If _ArrayCompare($arrayOne, $arrayThree) Then
    MsgBox(0, "", "$arrayOne and $arrayThree match!")
Else
    MsgBox(0, "", "$arrayOne and $arrayThree don't match!")
EndIf

Func _ArrayCompare($arrOne, $arrTwo)
    If UBound($arrOne) = UBound($arrTwo) Then
        ; Continue because the sizes match
        For $i = 1 to UBound($arrOne) - 1
            If $arrOne[$i] = $arrTwo[$i] Then
                ContinueLoop
            Else
                SetError(0)
                Return 0 ; Error - the array isn't the same
            EndIf
        Next
    Else
        SetError(1)
        Return 0 ; Error - the array isn't the same size
    EndIf
    Return 1; Loops matched
EndFunc

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