Sign in to follow this  
Followers 0
mlister

How to Compare two arrays?

12 posts in this topic

#1 ·  Posted (edited)

I have 2 arrays that I would like to compare, and if the value in $Array1 does not exist in $Array2, then add it to a txt file

I was thinking something like?

For $x = 1 to Ubound($Array1,1)
    $tmp = $Array1[$x]
     
     For $y = 1 to Ubound($Array2,1)
          $tmp2 = $Array2[$y]
          
           IF not StringInStr($tmp, $tmp2) Then
               FileWriteLine($new , $tmp & @CRLF)
          EndIf   

     Next
Next

I don't think the second array movement is right!?! But I'm lost now...... :)

Edited by mlister

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this...

For $x = 1 to Ubound($Array1) - 1
   $tmp = $Array1[$x]
   $tmp2 = $Array2[$x]
         
   If $tmp <> $tmp2 Then
      FileWriteLine($new , $tmp & @CRLF)
   EndIf   

Next
Edited by SlimShady

Share this post


Link to post
Share on other sites

looks good, and works! BUT... The Arrays do not have the same number of values.

At the moment $Array2 has only 3 values, whereas $Array1 has around 20. So I get an error because exceeded the $Array2 range.

Also, in the future this may be the other way around. i.e. $Array1 has less values than $Array2!

Any Ideas??

Thanks a lot

Share this post


Link to post
Share on other sites

You could just do some bounds checking...

For $x = 1 to Ubound($Array1) - 1
  
   If( $x > (UBound($Array2) - 1) ) Then ExitLoop

   $tmp = $Array1[$x]
   $tmp2 = $Array2[$x]
        
   If $tmp <> $tmp2 Then
      FileWriteLine($new , $tmp & @CRLF)
   EndIf   

Next

Hope that helps!

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Adding the bounds checking gets rid of the error - but the script doesn't really do what I need.

let me explain,

I need to check every value in $Array1 against all the values in $Array2, Logically ;

Check $Array1[1] against $Array2[1], $Array2[2], Etc....

And only FileWriteLine() if value in $Array1[1] does not exist in any value in $Array2.

Does that make sense?? Is it possible?

thanks for your help

Share this post


Link to post
Share on other sites

If that is the case then your original script was a lot closer than the one Slim put... Try this:

For $x = 1 to Ubound($Array1) - 1
   $tmp = $Array1[$x]
    
    For $y = 1 to Ubound($Array2) - 1
         $tmp2 = $Array2[$y]
         
          If NOT StringInStr($tmp, $tmp2) Then
              FileWriteLine($new , $tmp & @CRLF)
          EndIf

    Next
Next

These are both single dimension arrays correct?

That should work, Hope this helps!

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

Matt, Tried the script but still no joy.... (getting repeating lines in the txt file!)

The problem seems to be that it is not checking all values in $Array2 against the first value before adding the line.

If NOT StringInStr($tmp, $tmp2) Then
      FileWriteLine($new , $tmp & @CRLF)
EndIf

This code only checks against the current setting of $tmp2, not agaisn't all possible values in $Array2.

I've tried adding the below code to set a variable if found or not.

IF $tmp <> $tmp2 Then
      $chk = 1
Else
      $chk = 0
EndIf

Then only execute FileWriteLine() if $chk = 1.

This seems to work - but I'm still getting repeating lines! I'm working on it...

Share this post


Link to post
Share on other sites

There is an alternative, build a string out of all of the elements of the second array and do a stringinstr compared to each value in the first array.

I think that would look something like this:

Dim $tmpString

$tmpString = $Array2[1]
For $x = 2 to Ubound($Array2) - 1
    $tmpString = $tmpString & "|" & $Array2[$x]
Next

For $x = 1 to Ubound($Array1) - 1
   $tmp = $Array1[$x]
     
   If NOT StringInStr($tmpString, $tmp) Then
       FileWriteLine($new , $tmp & @CRLF)
   EndIf
   
Next

That should work at least conceptually.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Array indexing starts at 0 unless you created the array from StringSplit(), in which case, AutoIt provides a convience by having element[0] report the number of items in the array. If you aren't using StringSplit() to create your array(s), then the code needs to be changed to start from 0.

Edited by Valik

Share this post


Link to post
Share on other sites

You are right Valik.. I get mixed around while helping people with VB. Here is modified code that should work:

Dim $tmpString

$tmpString = $Array2[0]
For $x = 1 to Ubound($Array2) - 1
   $tmpString = $tmpString & "|" & $Array2[$x]
Next

For $x = 0 to Ubound($Array1) - 1
  $tmp = $Array1[$x]
    
  If NOT StringInStr($tmpString, $tmp) Then
      FileWriteLine($new , $tmp & @CRLF)
  EndIf
  
Next

Share this post


Link to post
Share on other sites

Matt, Code works greats - I am using StringSplit() to fill the arrays so I've changed it accordingly.

thanks for your help :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Very bad idea, Matt. What happens if $tmp happens to be a substring of one of the items in $tmpstring. You'll get a false result.

The nested loops are the way to go, they just need to start at 0, not 1.

Edit:

Maybe not, the wording was very confusing. I only figured out what they wanted to do by looking at the code. What you have posted, Matt, does seem to be what they want.

Edited by Valik

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