blumi Posted December 6, 2016 Share Posted December 6, 2016 I try to compare two arrays. The search values are in array1 and array2 is getting searched. If a value is found in the array, it should be deleted. It doesn't work and at the moment I can't see the error, need some help please. #include <Array.au3> Local $Array1[5] = [1,2,3,4,5] ; Array mit den Werten für die Suche Local $Array2[5] = [7,4,5,6,3] ; Array, dass durchsucht wird _ArrayDisplay($Array2) For $i = 0 To UBound($Array1)-1 For $j = 0 To UBound($Array2)-1 $found = _ArraySearch($Array2, $Array1[$i]) MsgBox(0, "", "i: " & $i & @CRLF & _ "j: " & $j & @CRLF & "Suchwert: " & $Array1[$i] & @CRLF & _ "Wert der durchsuchten Stelle: " & $Array2[$j] & @CRLF & _ "found: " & $found & @CRLF & _ "@error: " & @error) If($found <> -1) Then _ArrayDelete($Array2, $j) ;MsgBox(0, "", "Gelöscht: " & $j) ExitLoop EndIf Next Next _ArrayDisplay($Array2) Exit Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 6, 2016 Moderators Share Posted December 6, 2016 blumi, Too many loops - you only need the one: #include <Array.au3> Local $Array1[5] = [1,2,3,4,5] ; Array mit den Werten für die Suche Local $Array2[5] = [7,4,5,6,3] ; Array, dass durchsucht wird _ArrayDisplay($Array2) For $i = 0 To UBound($Array1)-1 $found = _ArraySearch($Array2, $Array1[$i]) If $found <> -1 Then _ArrayDelete($Array2, $found) Else ConsoleWrite($Array1[$i] & " not found" & @CRLF) EndIf Next _ArrayDisplay($Array2) Exit M23 SkysLastChance 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Terenz Posted December 6, 2016 Share Posted December 6, 2016 #include <Array.au3> Local $Array1[5] = [1, 2, 3, 4, 5] ; Array mit den Werten für die Suche Local $Array2[5] = [7, 4, 5, 6, 3] ; Array, dass durchsucht wird _ArrayDisplay($Array2) For $i = 0 To UBound($Array1) - 1 For $j = 0 To UBound($Array2) - 1 ConsoleWrite($Array1[$i] & " VS " & $Array2[$j] & @CRLF) If $Array1[$i] = $Array2[$j] Then _ArrayDelete($Array2, $j) ExitLoop EndIf Next Next _ArrayDisplay($Array2) Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted December 6, 2016 Moderators Share Posted December 6, 2016 @Terenz again, why waste a loop when it is not necessary? And your code only works if the item is the same at that index; it doesn't help a bit if the same item is located somewhere else in the array. "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 More sharing options...
Terenz Posted December 6, 2016 Share Posted December 6, 2016 (edited) @JLogan3o13 "Again" what? My code don't have _ArraySearch so i don't waste nothing and work based on the array of the OP. What do you mean by "only at same index"? 1 VS 7 1 VS 4 1 VS 5 1 VS 6 1 VS 3 2 VS 7 2 VS 4 2 VS 5 2 VS 6 2 VS 3 3 VS 7 3 VS 4 3 VS 5 3 VS 6 3 VS 3 Cutted output. For what i see it compare EVERY item of the first array with EVERY item of the second one. Edited December 6, 2016 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength Link to comment Share on other sites More sharing options...
j0kky Posted December 6, 2016 Share Posted December 6, 2016 (edited) Another fast solution which avoids _ArrayDelete function: #include <Array.au3> Local $Array1[5] = [1,2,3,4,5] Local $Array2[5] = [7,4,5,6,3] _ArrayDisplay($Array2) Func _ArrayCompare(ByRef $a1, ByRef $a2) Local $nOldSize = UBound($a2) Local $a3[$nOldSize], $nNewSize = $nOldSize For $i = 0 To UBound($a1) - 1 For $j = 0 To $nOldSize - 1 If Not $a3[$j] And ($a1[$i] = $a2[$j]) Then $a3[$j] = 1 $nNewSize -= 1 EndIf Next Next Local $a4[$nNewSize], $j = 0 For $i = 0 To $nOldSize - 1 If Not $a3[$i] Then $a4[$j] = $a2[$i] $j += 1 EndIf Next Return $a4 EndFunc _ArrayDisplay(_ArrayCompare($Array1, $Array2)) An array ($a3) can be saved if $Array2 could be edited. Edited December 6, 2016 by j0kky Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted December 6, 2016 Moderators Share Posted December 6, 2016 @Terenz, mea culpa, I went over your code too fast, so the statement about the index value was in error. My overall point, however, was that you're using multiple loops to accomplish what can be done in one. I was not denouncing your method, simply asking why - if there is a solution that does what is needed in fewer passes - would the OP benefit from going back to the two-step method. "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 More sharing options...
mikell Posted December 6, 2016 Share Posted December 6, 2016 Another funny - and faster - way #include <Array.au3> Local $Array1[5] = [1,2,3,4,5] ; Array mit den Werten für die Suche Local $Array2[5] = [7,4,5,6,3] ; Array, dass durchsucht wird _ArrayDisplay($Array2) Local $sd2 = ObjCreate("Scripting.Dictionary") For $i In $Array2 $sd2.Item($i) Next For $i In $Array1 $sd2.Remove($i) Next $Array2 = $sd2.Keys() _ArrayDisplay($Array2) j0kky 1 Link to comment Share on other sites More sharing options...
j0kky Posted December 6, 2016 Share Posted December 6, 2016 Cool way Mikell! I didn't know about its existence Spoiler Some UDFs I created: Winsock UDF STUN UDF WinApi_GetAdaptersAddresses _WinApi_GetLogicalProcessorInformation Bitwise with 64 bit integers An useful collection of zipping file UDFs Link to comment Share on other sites More sharing options...
blumi Posted December 7, 2016 Author Share Posted December 7, 2016 Thanks for all the fast help, now it works. Link to comment Share on other sites More sharing options...
mikell Posted December 7, 2016 Share Posted December 7, 2016 jOkky, The code was taken from this post FYI _ArrayUnique() uses this way too 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