aeun01 Posted November 5, 2022 Share Posted November 5, 2022 (edited) I am making a script to find out where a value in Array C is in Array A. It worked, but nested if statements are too long. Can this be represented simply by using for statement and putting a variable inside the array c[] with the following rules? #include <Array.au3> #include <MsgBoxConstants.au3> Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44] Local $c[]=['a4',22,44] For $b=0 To 17 If $c[0] == $a[$b] Then If $c[1] == $a[$b+1] Then If $c[2] == $a[$b+2] Then $k=$b EndIf EndIf EndIf Next MsgBox($MB_SYSTEMMODAL, "", $k &"th value" ) Edited November 5, 2022 by aeun01 Please don't copy&paste formating of translated text and post code in a codebox. Link to comment Share on other sites More sharing options...
OJBakker Posted November 5, 2022 Share Posted November 5, 2022 Something like the following code? #include <Array.au3> #include <MsgBoxConstants.au3> Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44] Local $c[]=['a4',22,44] For $ic=0 To Ubound($c) - 1 For $ia=0 To UBound($a) - 1 If $c[$ic]==$a[$ia] Then MsgBox($MB_SYSTEMMODAL, "", "value " & $c[$ic] & " found in $a at index " & $ia) EndIf Next Next aeun01 1 Link to comment Share on other sites More sharing options...
Solution RTFC Posted November 5, 2022 Solution Share Posted November 5, 2022 (edited) #include <MsgBoxConstants.au3> Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44] Local $c[]=['a4',22,44] Local $lastrow=UBound($a)-1 Local $lastcol=UBound($c)-1 Local $d=$c[0] & "/" For $cc=1 To $lastcol $d&= $c[$cc] & "/" ; this ensures 1/23 and 12/3 are not confused Next Local $e For $rc=0 To $lastrow-$lastcol ; no need to search beyond what would fit $e=$a[$rc] & "/" For $cc=1 To $lastcol $e&=$a[$rc+$cc] & "/" Next ; NB performing a STRING comparison on values (is this intended?) If $d==$e Then MsgBox($MB_SYSTEMMODAL, "", $rc &"th value (base-0)" ) Next A different way: #include <Array.au3> #include <MsgBoxConstants.au3> Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44] Local $c[]=['a4',22,44] Local $aStr=_ArrayToString($a) & "|" Local $cStr=_ArrayToString($c) & "|" Local $found=StringInStr($aStr,$cStr) if $found>0 Then Local $pos=0 For $b=1 To $found-1 $pos+=(StringMid($aStr,$b,1)="|") Next MsgBox($MB_SYSTEMMODAL, "", $pos &"th value (base-0)" ) EndIf Incorporating Nine's suggestion in the next post, this is shorter still (but does not contain a For-loop as requested by OP): #include <Array.au3> #include <MsgBoxConstants.au3> Local $a[]=[33,5,3,4,4,'a4',2,22,66,234,'a4',234,31,34,55,'a4',22,44] Local $c[]=['a4',22,44] Local $aStr=_ArrayToString($a) & "|" Local $found=StringInStr($aStr,_ArrayToString($c) & "|") If $found>0 Then StringReplace(StringLeft($aStr,$found-1),"|","|") MsgBox($MB_SYSTEMMODAL, "", (($found>0)?(@extended & "th value (base-0)"):("not found"))) EDIT: There was an edge-case that wasn't properly handled in all my examples (final substring part was unterminated so could trigger a false-positive), now fixed. Edited November 6, 2022 by RTFC aeun01 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Nine Posted November 5, 2022 Share Posted November 5, 2022 (edited) With no loop at all : #include <Array.au3> #include <Constants.au3> Local $a[] = [33, 5, 3, 4, 4, 'a4', 2, 22, 66, 234, 'a4', 234, 31, 34, 55, 'a4', 22, 44] Local $c[] = ['a4', 22, 44] Local $pos = _ArrayInArray($a, $c) If Not @error Then MsgBox($MB_SYSTEMMODAL, "", $pos & "th value (base-0)") Func _ArrayInArray(ByRef $a, ByRef $c) Local $aStr = _ArrayToString($a) Local $found = StringInStr($aStr, _ArrayToString($c)) If Not $found Then Return SetError(1, 0, -1) StringReplace(StringLeft($aStr, $found - 1), "|", "|", 0, $STR_CASESENSE) Return @extended EndFunc ;==>_ArrayInArray Edit : increase speed with removing case sensitivity Edited November 6, 2022 by Nine Change function name aeun01 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Gianni Posted November 5, 2022 Share Posted November 5, 2022 My 2 cents My 0.5 cents ... 😊 Local $a[] = ['a4', 22, 44, 33, 5, 3, 4, 4, 'a4', 22, 44, 66, 234, 'a4', 234, 31, 34, 55, 'a4', 22, 44] Local $c[] = ['a4', 22, 44] Local $bMatch For $ia = 0 To UBound($a) - UBound($c) $bMatch = 0 For $ic = 0 To UBound($c) - 1 $bMatch += ($a[$ia + $ic] == $c[$ic]) Next If $bMatch = UBound($c) Then ConsoleWrite("sequence found at index " & $ia & @CRLF) Next aeun01 1 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
aeun01 Posted November 8, 2022 Author Share Posted November 8, 2022 Sorry for checking late! Looking at the sources you wrote, thinking about how you wrote them, you learn a lot of different methods. thank you Link to comment Share on other sites More sharing options...
Developers Jos Posted November 8, 2022 Developers Share Posted November 8, 2022 (edited) On 11/5/2022 at 5:26 AM, aeun01 said: Please don't copy&paste formating of translated text and post code in a codebox. Guess you missed this from your first post? Please use the default formatting of the forum when posting text! Edited November 8, 2022 by Jos 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. Link to comment Share on other sites More sharing options...
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