Sign in to follow this  
Followers 0
gil900

Do I have a bug in my code?

13 posts in this topic

#1 ·  Posted (edited)

look at this code:

Func _IfArrayEqual($avArray1,$avArray2,$ArrayType = 1)
    $output = -1
    $Max_avArray1 = UBound($avArray1)-1
    $Max_avArray2 = UBound($avArray2)-1
    If $Max_avArray1 <> $Max_avArray2 Then
        $output = 1
    Else
        For $a = 1 To $Max_avArray1
            If $ArrayType = 1 Then $SearchVar = $avArray2[$a]
            If $ArrayType = 2 Then $SearchVar = $avArray2[$a][0]
            If _ArraySearch($avArray1,$SearchVar) = -1 Then
                $output = 1
                ExitLoop
            EndIf
            If $a = $Max_avArray1 Then $output = 0
        Next
    EndIf

    Return $output
EndFunc

i think that there is a case of bug.

my goal of this function is to compere between tow arrays and to tell if the arrays are equal or not.

it should Return 0 when the arrays are equal , Return 1 when the arrays are not equal and return -1 in case of failure.

the reason why I think there's a bug is because small part of my software didn't reacted right and i found that this function can be the only reason.

Unfortunately, I can not reproduce the bug. Because I deleted the database that led to the bug...

Edited by gil900

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Using _ArraySearch() is probably the cause of your problem. If the same data is found in a different array element, then the arrays may be different even though the result says they are the same. You need to test if $avArray1[$a]  is equal (or exaxctly equal ==) to $avArray2[$a]

Edited by czardas

Share this post


Link to post
Share on other sites

Using _ArraySearch() is probably the cause of your problem. If the same data is found in a different array element, then the arrays may be different even though the result says they are the same. You need to test if $avArray1[$a]  is equal (or exaxctly equal ==) to $avArray2[$a]

 

i don't see any logic problem. according to this function "equal" mean that tow arrays contain exactly the same data. the order of the data is not important.

 

this is example how the logic work on the following arrays:

example 1:

1: a - d

2: b - a

3: c - a

4: d - c

5: e - d

Local $avArray1[6]
$avArray1[1] = "a"
$avArray1[2] = "b"
$avArray1[3] = "c"
$avArray1[4] = "d"
$avArray1[5] = "e"

Local $avArray2[6]
$avArray2[1] = "e"
$avArray2[2] = "d"
$avArray2[3] = "c"
$avArray2[4] = "b"
$avArray2[5] = "a"

for a = 1 to 5

loop 1: a found from $avArray1 found in $avArray2

loop 2: b not found from $avArray1 in $avArray2  =>

ExitLoop => return 0 ( Arrays not equal )

if loop number = 5 then return 1 ( Arrays are equal)

next

example2:

1: a - e

2: b - d

3: c - b

4: d - c

5: e - a

Local $avArray1[6]
$avArray1[1] = "a"
$avArray1[2] = "b"
$avArray1[3] = "c"
$avArray1[4] = "d"
$avArray1[5] = "e"

Local $avArray2[6]
$avArray2[1] = "e"
$avArray2[2] = "d"
$avArray2[3] = "b"
$avArray2[4] = "c"
$avArray2[5] = "a"

for 1 to 5

loop1: a found from $avArray1 found in $avArray2

loop2: b found from $avArray1 found in $avArray2

loop3: c found from $avArray1 found in $avArray2

loop4: d found from $avArray1 found in $avArray2

loop5: e found from $avArray1 found in $avArray2

if loop number = 5 then return 1 ( Arrays are equal)

next

 

The code works as expected

Share this post


Link to post
Share on other sites

i found the bug.

it is not the function.

it was about how i created Array1.

i part 1 in my code did a some filtering process when it reading the data from text document.

and part 2 did  a little different filtering process.

so that's why something did not work right

Share this post


Link to post
Share on other sites

Hi gil900

wait a minute..... do not put your script in production!
pay attention that your logic is wrong
try this, your script says the 2 arrays are equal!

#include <array.au3>
Local $avArray1[6]
$avArray1[1] = "a"
$avArray1[2] = "b"
$avArray1[3] = "c"
$avArray1[4] = "d"
$avArray1[5] = "e"

Local $avArray2[6]
$avArray2[1] = "a"
$avArray2[2] = "a"
$avArray2[3] = "a"
$avArray2[4] = "a"
$avArray2[5] = "a"

ConsoleWrite(_IfArrayEqual($avArray1, $avArray2) @CRLF)

Func _IfArrayEqual($avArray1, $avArray2, $ArrayType = 1)
    $output = -1
    $Max_avArray1 = UBound($avArray1) - 1
    $Max_avArray2 = UBound($avArray2) - 1
    If $Max_avArray1 <> $Max_avArray2 Then
        $output = 1
    Else
        For $a = 1 To $Max_avArray1
            If $ArrayType = 1 Then $SearchVar = $avArray2[$a]
            If $ArrayType = 2 Then $SearchVar = $avArray2[$a][0]
            If _ArraySearch($avArray1, $SearchVar) = -1 Then
                $output = 1
                ExitLoop
            EndIf
            If $a = $Max_avArray1 Then $output = 0
        Next
    EndIf

    Return $output
EndFunc

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

i don't see any logic problem. according to this function "equal" mean that tow arrays contain exactly the same data. the order of the data is not important.

 

Ha! That's like saying the number 1920 is the same as 2091 and expectimg people to understand because the order of numbers does not matter. Define exactly what you want the function to do. Are repeats allowed? Do different numbers of repeats in different positions matter? Is 1122223 the same as 1233333?

Edited by czardas

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

i don't see any logic problem. according to this function "equal" mean that tow arrays contain exactly the same data. the order of the data is not important.

"number 1920 is the same as 2091"

gota agree with czards on thisone aldo he do have If $Max_avArray1 <> $Max_avArray2 Then

so case is first array contain

[1] = 111

[2] = bbb

[3] = bbb

and other contain

[1] = 111

[2] = 111

[3] = bbb

are they equal or not using arraysearch? :)

so gil900 i think your logic have problems with search and with speed :P

something similar to this shud work if abowe is needed, so to use comparing instead search and adding unique if needed before sorting is your choice

 

Func _IfArrayEqual($avArray1, $avArray2, $ArrayType = 1)
    $output = -1
    $Max_avArray1 = UBound($avArray1) - 1
    $Max_avArray2 = UBound($avArray2) - 1
    If $Max_avArray1 <> $Max_avArray2 Then
        Return '+>NOT OK one is larger than another!!!'
    Else
        _ArraySort($avArray1)
        _ArraySort($avArray2)
       If $ArrayType = 1 Then
            For $a = 0 To $Max_avArray1
                If $avArray1[$a] <> $avArray2[$a] Then Return '->NOT OK, data problem ("'&$avArray1[$a]&'") vs ("'&$avArray2[$a]&'") line '&$a
            Next
        Else
            For $a = 0 To $Max_avArray1
                ;some code for everything else
            Next
        EndIf
    EndIf
    Return '!>Everything is OK'
EndFunc
Edited by bogQ
1 person likes this

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Ha! That's like saying the number 1920 is the same as 2091 and expectimg people to understand because the order of numbers does not matter. Define exactly what you want the function to do. Are repeats allowed? Do different numbers of repeats in different positions matter? Is 1122223 the same as 1233333?

It depends on how you intend to use the information

"1122223" is not "2132122"

but 1+1+2+2+2+2+3 is equal to 2+1+3+2+1+2+2

Share this post


Link to post
Share on other sites

Local $avArray1[6]
$avArray1[1] = "a"
$avArray1[2] = "b"
$avArray1[3] = "c"
$avArray1[4] = "d"
$avArray1[5] = "e"

; Should be:

Local $avArray1[6]
$avArray1[0] = "a"
$avArray1[1] = "b"
$avArray1[2] = "c"
$avArray1[3] = "d"
$avArray1[4] = "e"


"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post


Link to post
Share on other sites

This function is confirming that array2 consists only of characters that appear in array1.  Problem is that they all the chars in array1 don't have to be in array2 for it to succeed.

Share this post


Link to post
Share on other sites

thank you very much for the help!!

i know that this function was the problem.

when i said that i found the bug, i was worng.. it was about something else.

bug was indead in my function so thank you

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

OKay. Your desciption was a little confusing. You should declare $output, $Max_avArray1 and $Max_avArray2 using the keyword Local. Also look at the second parameter for the function Ubound. particularly see what this does ==> Ubound($avArray1, 0)

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