Jump to content

Recommended Posts

Posted

i'm in need of a function that compares 2 arrays (1 dimentional) and returns the difference.

for example: a1(1,2,3,4) vs a2(1,3) would return array (2,4).

i could not find it via search. should i write my own or is there such a thing already?

Posted

Are you looking for a set union, intersection, disjoint functions?

Dim $a[6] = [1, 3, 5, 7, 9, 11]
Dim $b[4] = [1, 2, 3, 4]
Dim $c = _SetUnion($a, $b)  ; Gets [1, 2, 3, 4, 5, 7, 9, 11]
Dim $d = _SetIntersection($a, $b)  ; Gets [1, 3]
Dim $e = _SetMissing($a, $b)  ; What elements of $a are missing in $b.  Gets [5, 7, 9, 11]

Go ahead and write them. :(

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Posted (edited)

i'm in need of a function that compares 2 arrays (1 dimentional) and returns the difference.

for example: a1(1,2,3,4) vs a2(1,3) would return array (2,4).

i could not find it via search. should i write my own or is there such a thing already?

<{POST_SNAPBACK}>

Ok, here is my first UDF.

#include <Array.au3>

Dim $a
$a = _ArrayCreate(1, 3, 5, 7, 9, 11)
Dim $b
$b = _ArrayCreate(1, 2, 3, 4)

$Diff = ArrayDiff($a, $b)
MsgBox(0,"",$Diff)

Exit

Func ArrayDiff($a, $b)  
    Local $Diff = ""
    
    For $L0 = 1 to 2    
        for $L1 = 0 to UBound($a) - 1
            $Found = 0
            for $L2 = 0 to UBound($b) - 1
                if $a[$L1] = $b[$L2] Then
                    $Found = 1
                    ExitLoop
                EndIf       
            Next            
            if $Found = 0 Then
                $Diff = $Diff & "," & $a[$L1]
            EndIf       
        Next
        if $L0 = 1 Then
            $Temp = $a
            $a = $b
            $b = $Temp      
        EndIf   
    Next
    
    $Diff = StringTrimLeft($Diff,1) 
    $Diff = StringSplit($Diff,",")
    _ArraySort($Diff)
    $Diff = _ArrayToString($Diff,",")
    
    Return $Diff
EndFunc

Hope everyone likes it.

Note: One strange thing is that _ArraySort puts 11 before 2.

vbMark

Edited by vbMark
Posted (edited)

Are you looking for a set union, intersection, disjoint functions?

actually yes, it's more like sets. thanx man.

i did not find this UDF in 3.1.0 help though. i guess i should have checked the UDF thread :(

EDIT> now i'm confused.. are you referring to previously written functions? i can't find it. (i interpreted the wink as if they're already written) :(

Edited by dooshorama
  • Developers
Posted

[quote=vb script:

#include <array.au3>
dim $tmp[6]
$tmp[0] = "2"
$tmp[1] = "11"
$tmp[2] = "05"
$tmp[3] = "0"
$tmp[4] = "5"
$tmp[5] = "19"
_ArraySort($tmp,0,0)
_ArrayDisplay($tmp,'test1')
$tmp[0] = 2
$tmp[1] = 11
$tmp[2] = 05
$tmp[3] = 0
$tmp[4] = 5
$tmp[5] = 19
_ArraySort($tmp,0,0)
_ArrayDisplay($tmp,'test2')

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted

If its just a number in the array, and as above, it may or may not have quotation marks around it you can use...

if Number($a[$L1]) = Number($b[$L2]) Then

qq

Posted

actually yes, it's more like sets. thanx man.

i did not find this UDF in 3.1.0 help though. i guess i should have checked the UDF thread :(

EDIT> now i'm confused.. are you referring to previously written functions? i can't find it. (i interpreted the wink as if they're already written) :

<{POST_SNAPBACK}>

The wink is indicating that my advice is not really being that helpful. Maybe I should have used :( . I do not know if they have been written or not. As for some help writing: Sort the arrays and look for common elements in the two arrays.

; Note: Untested

Func AddElem(ByRef $array, byref $active, $value)

$active = $active+1

If $active >= UBound($array) Then ReDim $array[$active+9]

$array[$active] = $value

endfunc

Func _SetUnion(ByRef $a1, ByRef $a2)

Local $RetVal[10], $active = -1

Local $i1=0, $i2=0

; Verify that the arguments are arrays

If IsArray($a1) and IsArray($a2) then

; Assuming one-dimensional arrays. Yeah, I know about making assumptions.

; Assuming sorted arrays with unique elements

While $i1 < UBound($a1) or $i2 < UBound($a2)

if $a1[$i1] < $a2[$i2] Then

AddElem($retval, $active, $a1[$i1])

$i1 = $i1 + 1

elseif $a1[$i1] > $a2[$i2] Then

AddElem($retval, $active, $a2[$i2])

$i2 = $i2 + 1

else ; $a1[$i1] = $a2[$i2]

AddElem($retval, $active, $a1[$i1])

$i1 = $i1 + 1

$i2 = $i2 + 1

endif

Wend

ElseIf IsArray($a1) Then

; $a2 is not an array

$i2 = 0

for $i1 = 0 to UBound($a1)-1

if $a2 > $a1[$i1] then

AddElem($retval, $active, $a1[$i1])

else

AddElem($retval, $active, $a2)

$i2 = 1

endif

Next

If $i2 = 0 then

AddElem($retval, $active, $a2)

endif

ElseIf IsArray($a2) Then

; $a1 is not an array

$i2 = 0

for $i1 = 0 to UBound($a2)-1

if $a1 > $a2[$i1] then

AddElem($retval, $active, $a2[$i1])

else

AddElem($retval, $active, $a1)

$i2 = 1

endif

Next

If $i2 = 0 then

AddElem($retval, $active, $a1)

endif

Else

; Neither is an array

if $a1 < $a2 then

AddElem($retval, $active, $a1)

AddElem($retval, $active, $a2)

elseif $a1 > $a2 then

AddElem($retval, $active, $a2)

AddElem($retval, $active, $a1)

else

AddElem($retval, $active, $a1)

endif

Endif

if $active = -1 then

$retval = ""

else

ReDim $retval[$active+1]

endif

ReDim $retval[$active+1]

Return $retval

EndFunc

Func _SetIntersection(ByRef $a1, ByRef $a2)

Local $RetVal[10], $active = -1

Local $i1=0, $i2=0

; Verify that the arguments are arrays

If IsArray($a1) and IsArray($a2) then

; Assuming one-dimensional arrays. Yeah, I know about making assumptions.

; Assuming sorted arrays with unique elements

While $i1 < UBound($a1) or $i2 < UBound($a2)

if $a1[$i1] < $a2[$i2] Then

$i1 = $i1 + 1

elseif $a1[$i1] > $a2[$i2] Then

$i2 = $i2 + 1

else ; $a1[$i1] = $a2[$i2]

AddElem($retval, $active, $a1[$i1])

$i1 = $i1 + 1

$i2 = $i2 + 1

endif

Wend

ElseIf IsArray($a1) Then

; $a2 is not an array

for $i1 = 0 to UBound($a1)-1

if $a2 = $a1[$i1] then

AddElem($retval, $active, $a2)

exitloop

endif

Next

ElseIf IsArray($a2) Then

; $a1 is not an array

for $i1 = 0 to UBound($a2)-1

if $a1 = $a2[$i1] then

AddElem($retval, $active, $a1)

Next

Else

; Neither is an array

if $a1 = $a2 then

AddElem($retval, $active, $a1)

endif

Endif

if $active = -1 then

$retval = ""

else

ReDim $retval[$active+1]

endif

Return $retval

EndFunc

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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
  • Recently Browsing   0 members

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