Jump to content

Generate 3rd array based on contents of 2 others


Recommended Posts

Given arrays 1 and 2, does anyone have any suggestions on how to create array 3 which contains everything from array 1 except that which is in array 2?

Example array 1

a b c d e f g

Example array 2

d e b

Example array 3

a b c f g

Link to comment
Share on other sites

Loops and arrays goes hand in hand :)

#include <array.au3>
Dim $a1[5]=[1,4,7,10,23]
Dim $a2[2]=[4,10]
Dim $a3[1]
For $i=0  To UBound($a1)-1
    For $j=0 To Ubound($a2)-1
        If $a1[$i]=$a2[$j] Then ContinueLoop 2
    Next
    _ArrayAdd($a3,$a1[$i])
Next
_ArrayDelete($a3,0)
_ArrayDisplay($a3)

:(

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Here's the same thing without Array.au3 (I'm currently trying to avoid it in my scripts since it's horrible slow):

Dim $string
Dim $a1[5]=[1,4,7,10,23]
Dim $a2[2]=[4,10]
Dim $a3[1]
For $i=0  To UBound($a1)-1
    For $j=0 To Ubound($a2)-1
        If $a1[$i]=$a2[$j] Then ContinueLoop 2
    Next
    ReDim $a3[UBound($a3)+1]
    $a3[UBound($a3)-1]=$a1[$i]
Next
For $h=1 To UBound($a3)-1
    $string&=$h&": "&$a3[$h]&@CRLF
Next
MsgBox(0,"Array Display",$string)

:)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Thank you very much guys. For some reason my brain just wasn't working this afternoon.

Now let me ask if I'm approaching my task in the best way possible.

Array1 is actually a list of all files in a directory.

Array2 is a subset of Array1.

I want to delete all files except for those in Array2.

My first thought was to create a third array as described above and then delete those files.

Thoughts?

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...