Sign in to follow this  
Followers 0
Zomp

how to compare two arrays, maybe a trivial question

6 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

As far as I know, you have to compare elements individually.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

That code you posted is incorrect. You ended up with two Nexts and I have no idea what that Return is doing for you.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

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