Jump to content

compare two arrays


blumi
 Share

Recommended Posts

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

#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

  • Moderators

@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

@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 by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

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 by j0kky
Link to comment
Share on other sites

  • Moderators

@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

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)

 

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