Renderer Posted April 21, 2019 Posted April 21, 2019 Hello guys, I've got a little question: How do I check if the values or some of the values from an array ($alphabet) are also in the other array ($word)? Like: $alphabet[26] = [A,B,C,D,E,F,G, ...] $word = [H,E,L,L,O] How do I check if the values from $alphabeth (in this case H,E,L,L,O) are in the array called $word? Thanks in advance!
FrancescoDiMuro Posted April 21, 2019 Posted April 21, 2019 @Renderer _ArraySearch(), _ArrayFindAll(), _ArrayToString() Click here to see my signature: Reveal hidden contents ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Malkey Posted April 22, 2019 Posted April 22, 2019 In this example:- The function IsArray1SomeInArray2() answers your first question of post #1. The function IsArray1InArray2() answers your second question of post #1. An optional case sensitivity parameter is added to answer one of my questions. #include <Array.au3> Local $sAlphabet = "ABCDEFGHIJKLMOPQRSTUVWXYZ" $aAlphabet = StringSplit($sAlphabet, "", 2) Local $sWord = "hello" ; "HELLO" ; "Hello" ; $aWord = StringSplit($sWord, "", 2) MsgBox(0, "IsArray1SomeInArray2()", "One or more of the elements of Array1" & (IsArray1SomeInArray2($aWord, $aAlphabet, 0) ? " are in " : " are NOT in ") & "Array2") MsgBox(0, "IsArray1InArray2()", "All the elements of Array1" & (IsArray1InArray2($aWord, $aAlphabet, 0) ? " are in " : " are NOT in ") & "Array2") ; Checks if some of the values from array1 ($word) are also in the array2 ($alphabet); also, ; Checks if some of the values from array1 ($alphabet) are also in the array2 ($word). Func IsArray1SomeInArray2($Array1, $Array2, $iCase = 0) ; $iCase = 0 (case insensitive, "h" = "H") $Array1 = _ArrayUnique($Array1, 0, 0, $iCase, $ARRAYUNIQUE_NOCOUNT) $Array2 = _ArrayUnique($Array2, 0, 0, $iCase, $ARRAYUNIQUE_NOCOUNT) Return UBound(StringRegExp(_ArrayToString($Array2), ($iCase ? "" : "(?i)") & _ArrayToString($Array1), 3)) > 0 EndFunc ;==>IsArray1SomeInArray2 ; Checks if all of the values from array1 ($word) are also in the array2 ($alphabet) Func IsArray1InArray2($Array1, $Array2, $iCase = 0) $Array1 = _ArrayUnique($Array1, 0, 0, $iCase, $ARRAYUNIQUE_NOCOUNT) $a = StringRegExp(_ArrayToString($Array2), ($iCase ? "" : "(?i)") & _ArrayToString($Array1), 3) Return UBound($Array1) = UBound($a) EndFunc ;==>IsArray1InArray2 If you do not understand something in either function, I suggest you use as many ConsoleWrite( [Insert variable here] & @CRLF)'s as needed, together with AutoIt's help file. Just as I have done, do, and will do. In SciTE, I use "cw ", that is, "cw" followed by a space - a shortcut. FrancescoDiMuro and Fr33b0w 2
mikell Posted April 22, 2019 Posted April 22, 2019 An other way could be Scripting.Dictionary ;Local $sAlphabet = "ABCDEFGHIJKLMOPQRSTUVWXYZ" Local $sAlphabet = "ABCDEFGHIJKL" $aAlphabet = StringSplit($sAlphabet, "", 2) Local $sWord = "Hello" $aWord = StringSplit($sWord, "", 2) Local $sdAlphabet = ObjCreate("Scripting.Dictionary"), $count $sdAlphabet.CompareMode = 1 ; case insensitive For $i In $aAlphabet $sdAlphabet.Item($i) ; populate the dictionary Next For $i In $aWord If $sdAlphabet.Exists($i) Then $count += 1 ; then check Next Msgbox(0,"", $count & " values out of " & UBound($aWord) & " from $aWord exist in $aAlphabet") Fr33b0w 1
iamtheky Posted April 22, 2019 Posted April 22, 2019 This behavior might not be 100% kosher #include<array.au3> local $aLphabet[8] = ['A','B','D','F','G','H','I' ,'O'] local $aWord[5] = ['H','E','L','L','O'] _ArrayDisplay(StringRegExp(_ArrayToString($aWord , ',') , _ArrayToString($aLphabet , '|') , 3) , 'Matches') Reveal hidden contents ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Gianni Posted April 22, 2019 Posted April 22, 2019 in the array returned by this function you will have: in column 1, elements belonging only to array 1; in column 2, elements belonging only to array 2; in column 3, elements belonging to both arrays p.s. it's a case sensitive function expandcollapse popup#include <Array.au3> Local $sAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" $aAlphabet = StringSplit($sAlphabet, "", 2) Local $sWord = "HELLO" ; "hello" ; "Hello" ; $aWord = StringSplit($sWord, "", 2) _ArrayDisplay(_Separate($aAlphabet, $aWord)) 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 reference: https://www.autoitscript.com/forum/topic/164728-compare-2-arrays-with-3-arrays-as-a-result/?do=findComment&comment=1202060 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
iamtheky Posted April 22, 2019 Posted April 22, 2019 it seems once a year we all gather to revisit this topic Gianni 1 Reveal hidden contents ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Gianni Posted April 22, 2019 Posted April 22, 2019 On 4/22/2019 at 4:17 PM, iamtheky said: it seems once a year we all gather to revisit this topic Expand Indeed . Don't know if it's for laziness or for conenience, but I prefer don't reinvent the wheel when I can Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
mikell Posted April 22, 2019 Posted April 22, 2019 On 4/22/2019 at 4:17 PM, iamtheky said: it seems once a year we all gather to revisit this topic Expand Yes, it could be pinned Guess where does my previous code come from ?
Deye Posted April 22, 2019 Posted April 22, 2019 (edited) Not the cleanest example yet but serves it well #include <Array.au3> MsgBox(262144, 'is_FullMatch', _Match("ABCDEFGHIJKLMOPQRSTUVWXYZ", "HELLO")) Func _Match($sStringFrom, $sWord) Local $aWord = StringSplit($sWord, "", 3) $sWord = _ArrayToString(_ArrayUnique($aWord, 0, 0, 0, 0), "") Local $Ret = UBound(StringRegExp("|" & _ArrayToString(StringSplit($sStringFrom, "", 3)), "(?i:[|" & $sWord & "])+\w", 3)) MsgBox(262144, "", StringLen($sStringFrom) - $Ret & " Unique Matches from " & StringLen($sWord)) Return StringLen($sWord) = StringLen($sStringFrom) - $Ret EndFunc ;==>_Match Deye Edited April 22, 2019 by Deye obsolete line
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now