Jump to content

how to compare two arrays, maybe a trivial question


Zomp
 Share

Recommended Posts

Suppose I have $Array1[1000] and $Array2[1000]

Which is the fastest and simplest way to check if they (i.e. all their elements) are equal?

Have I to write

$equal=true
for $i=0 to 999
   if not $Array1[$i]=$Array2[$i] then
      $equal=false
      exitloop
   endif
next
consolewrite($equal @LF)

or is there something better?

Edited by Zomp
Link to comment
Share on other sites

I believe that a small correction help you:

$equal=true
for $i=0 to 999
   if not $Array1[$i]=$Array2[$i] then
      $equal=false
      exitloop
   endif
next
consolewrite($equal @LF)

Try This:

$equal=true
for $i=0 to 999
   if not $Array1[$i]=$Array2[$i] then
      $equal=false
      exitloop
   endif
consolewrite("Element " & $i & ": " & $equal & @CRLF)
next

Edited by DarkwarlorD
Link to comment
Share on other sites

Try something like this, to be honest I've never used For...In loops before until today so I'm not sure it will work.

Local $i = 0
FOR $element IN $aArray
If $test = $element Then Return 0
        $i = $i + 1
NEXT
Return 1

Edit: typo

Edited by Pain
Link to comment
Share on other sites

Sorry, I removed one of the Nexts. What I posted is a part (a function) of one of my scripts where I compare the elements in a array and Return true or false if there was any duplicated elements. As I said, I'm not sure it will work.

nvm I just noticed my function only found duplicated element in same array and not in two.

anyways here is a example of the function I'm using in my script

Dim $Array[5] = [1,2,3,4,5]
Global $test = 1;6

compare()

Func compare()
    Local $i = 0
    For $element in $Array
        If $test = $element Then MsgBox (0,"","Duplicated element!")
        $i = $i + 1
    Next
EndFunc
Edited by Pain
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...