lattey

autoit: Check if every element in first array exist in second array

12 posts in this topic

#1 ·  Posted

i have 2 different array as below:

   

array1[10] = ("1","2","3","4","5","6","7","8","9","10")
array2    = ("|","","1","2","4","5")

how can i compare each element in array2 is exist in array1 and sort according to array1 and detect if element array1 = "2" is not exist in array2 while ignoring array1  from "6" - "10" elements?
 

Share this post


Link to post
Share on other sites



#2 ·  Posted

Maybe:

#include <Array.au3>

Local $aTarget[1]
Local $aSource[10] = ["1","2","3","4","5","6","7","8","9","10"]
Local $aSearch[7]    = ["|","","1","2","4","5", "0"]
_ArraySort($aSearch) ;~ Sort Search Order

For $i = 0 To UBound($aSearch) - 1
    If $aSearch[$i] = "" Then ContinueLoop
    If _ArraySearch($aSource, $aSearch[$i]) >= 0 Then _ArrayAdd($aTarget, $aSearch[$i])
Next
$aTarget[0] = UBound($aTarget) - 1
_ArrayDisplay($aTarget)

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

There is also this UDF, which includes an _ArrayCompare function:

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#4 ·  Posted

sorry i dont how to reply Subz's post. 

what if the element 2 and 3 not exist in $aSearch? how can i detect the element 2 and 3 not exist?

Local $aSearch[7]    = ["|","","1","4","5", "0"]

Share this post


Link to post
Share on other sites

#5 ·  Posted

Don't really understand the question, basically one array is the search criteria, in my example $aSearch is the criteria, you want to check if each array item in $aSearch exists in $aSource and if it does, it writes it to $aTarget.  Now if you only want to return an array of items not found, you can change the following which will show you all the values that weren't found in $aSource.

If _ArraySearch($aSource, $aSearch[$i]) < 0 Then _ArrayAdd($aTarget, $aSearch[$i])

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

this question seems similar to this other post,

In post #13 there is the answer I proposed in that case, I post here again to compare your 2 arrays:

( col1 contains values contained only in first array, col2 those contained only in the second one, and col3 in both )

#include <Array.au3>

Local $a[] = ["1","2","3","4","5","6","7","8","9","10"]
Local $b[]   = ["|","","1","2","4","5"]

_ArrayDisplay(_Separate($a, $b))

Func _Separate(ByRef $in0, ByRef $in1)
    $in0 = _ArrayUnique($in0, 0, Default, Default, 0)
    $in1 = _ArrayUnique($in1, 0, Default, Default, 0)
    Local $z[2] = [UBound($in0), UBound($in1)], $low = 1 * ($z[0] > $z[1]), $aTemp[$z[Not $low]][3], $aOut = $aTemp, $aNdx[3]
    For $i = 0 To $z[Not $low] - 1
        If $i < $z[0] Then $aTemp[$i][0] = $in0[$i]
        If $i < $z[1] Then $aTemp[$i][1] = $in1[$i]
    Next
    For $i = 0 To $z[$low] - 1
        $x = _ArrayFindAll($aTemp, $aTemp[$i][$low], 0, 0, 1, 0, Not $low)
        If Not @error Then ; both
            For $j = 0 To UBound($x) - 1
                $aTemp[$x[$j]][2] = 1
            Next
            $aOut[$aNdx[2]][2] = $aTemp[$i][$low]
            $aNdx[2] += 1
        Else ; only in $low
            $aOut[$aNdx[$low]][$low] = $aTemp[$i][$low]
            $aNdx[$low] += 1
        EndIf
    Next
    For $i = 0 To $z[Not $low] - 1
        If $aTemp[$i][2] <> 1 Then
            $aOut[$aNdx[Not $low]][Not $low] = $aTemp[$i][Not $low]
            $aNdx[Not $low] += 1
        EndIf
    Next
    ReDim $aOut[_ArrayMax($aNdx)][3]
    Return $aOut
EndFunc   ;==>_Separate

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#7 ·  Posted

21 hours ago, lattey said:

.... and sort according to array1 ....

This example focuses on sorting an array according to another array.

#include <Array.au3>

Local $Array1[10] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] ; Sort order
Local $Array2[7] = ["|", "", "1", "2", "4", "5", "0"] ; The array to be sorted.

_ArraySortInOrder($Array2, $Array1)
_ArrayDisplay($Array2, "Sorted Array")


; Sort an array ($ArrayToSort) in the same order as another array ($aOrder) is ordered.
; Sorts on the $aOrder array's indexes that are returned from _ArraySearch() function.
;
Func _ArraySortInOrder(ByRef $ArrayToSort, $aOrder)
    Local $aSort[UBound($ArrayToSort)][2] ; Create temporary 2D array for sorting purposes.
    Local $iIndex, $iCount = 100000
    For $i = 0 To UBound($ArrayToSort) - 1
        $iIndex = _ArraySearch($aOrder, $ArrayToSort[$i])
        $aSort[$i][0] = ($iIndex >= 0 ? $iIndex : $iCount + $i) ; Unfound elements in $aOrder retain same order as in $ArrayToSort, but, at the end of the sorted $ArrayToSort array.
        $aSort[$i][1] = ($iIndex >= 0 ? $ArrayToSort[$i] : $ArrayToSort[$i] & " - Does not exist in Array1") ;
        ;$aSort[$i][1] = $ArrayToSort[$i]
    Next
    _ArraySort($aSort)

    ; Copy the 2nd column of the 2D array into a 1D array.
    For $i = 0 To UBound($aSort) - 1
        $ArrayToSort[$i] = $aSort[$i][1]
    Next
EndFunc   ;==>_ArraySortInOrder

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

actually, what i mean is, i have 2 different array right

local $arraySource [10] = ["1","2","3","4","5","6","7","8","9","10"]
local $arraySerch [6]= ["|","","1","2","4","5"]

what i need is to detect after compare both array, i need to tell user that element no 3 in $arraySource is not exist in $arraySearch

Share this post


Link to post
Share on other sites

#9 ·  Posted

lattey,

Answered several times.  Do the above solutions not word for you?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#10 ·  Posted

sorry, i have problem to understand the code. can you please clarify which solution can i use?

Share this post


Link to post
Share on other sites

#11 ·  Posted

You can use any of the solutions presented. Pick one, try it for yourself, and see which works best for your situation.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#12 ·  Posted

thanks guys. i already resolved it using one of the solutions above.:)

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