Jump to content

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


Recommended Posts

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?
 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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])

 

Link to comment
Share on other sites

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

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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