Jump to content

My code isn't deleting strings from an Array?


Recommended Posts

Dim $OldFoundMatchesUsernames[1] = ["Empty"]
Global $BlackList[3] = ["8809708", $Username, "thakhy"]

While 1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$FoundUsernames = StringRegExp($MatchesHTML, 'Picture of (.*?)"', 3)
    $FoundMatchesUsernames = _ArrayUnique($FoundUsernames)
    _ArrayDelete($FoundMatchesUsernames, 0)
    For $j = 0 To UBound($BlackList) - 1
        For $i = UBound($FoundMatchesUsernames) - 1 To 1 Step -1
            If String($BlackList[$j]) = String($FoundMatchesUsernames[$i]) Then
                _ArrayDelete($FoundMatchesUsernames, $i)
                ExitLoop ;only when sure each Element in $FoundMatchesUsernames is unique
            Else
            EndIf
        Next
    Next
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    If Not $OldFoundMatchesUsernames[0] == "Empty" Then
        For $j = 0 To UBound($OldFoundMatchesUsernames) - 1
            For $i = UBound($FoundMatchesUsernames) - 1 To 1 Step -1
                If String($OldFoundMatchesUsernames[$j]) = String($FoundMatchesUsernames[$i]) Then
                    _ArrayDelete($FoundMatchesUsernames, $i)
                    ExitLoop ;only when sure each Element in $FoundMatchesUsernames is unique
                Else
                EndIf
            Next
        Next
    EndIf
    
    
    
    Dim $OldFoundMatchesUsernames = $FoundMatchesUsernames
    Wend

So, the first Part of the code that is within the ;s works perfectly fine in removing the strings inside of BlackList from the array FoundMatchesUsernames.

 

But for some reason, I'm not able to delete the strings in FoundMatchesUsernames that are found in OldFoundMatchesUsernames

Edited by Dgameman1
Link to comment
Share on other sites

Wouldn't it be easier and faster to lose the for next loop entirely and replace it with these 2 lines of code:

_ArrayAdd($FoundMatchesUsernames,$OldFoundMatchesUsernames);combines the 2 arrays into 1.
$FoundMatchesUsernames=_ArrayUnique($FoundMatchesUsernames);Uniques the array.

 

Edited by BetaLeaf

 

 

Link to comment
Share on other sites

4 hours ago, mikell said:

Because of the operators precedence, this

If Not $OldFoundMatchesUsernames[0] == "Empty" Then

is evaluated like this

If (Not $OldFoundMatchesUsernames[0]) == "Empty" Then

so it should (probably...) be written like this

If Not ($OldFoundMatchesUsernames[0] == "Empty") Then

 

I changed it and you're 100% correct. I feel like an idiot lol.

So in some cases, $FoundMatchesUsernames is going to have the same stuff as $OldFoundMatchesUsernames the second time through.

The only issue is that the first row is still there instead of it all being removed?

Link to comment
Share on other sites

7 hours ago, BetaLeaf said:

If you are working with large arrays, you may want to consider my solution for optimization purposes. 

The best optimization by far would be to use Scripting.Dictionary  :)

#include <Array.au3>

local $BlackList = ["xxx", "yyy", "zzz"]
local $OldFoundMatchesUsernames = ["aaa", "bbb", "ccc", "ddd"]
local $FoundMatchesUsernames = ["yyy", "ggg", "aaa", "eee", "xxx", "ddd", "ccc", "zzz", "fff"]

 ; create dictionary
Local $sdFoundMatchesUsernames = ObjCreate("Scripting.Dictionary")
; $sdFoundMatchesUsernames.CompareMode = 1   ; case insensitive (if needed)

 ; build the dictionary (removing duplicates)
For $i In $FoundMatchesUsernames 
    $sdFoundMatchesUsernames.Item($i)
Next
 ; remove blacklisted
For $i In $BlackList
    $sdFoundMatchesUsernames.Remove($i) 
Next
 ; add $OldFoundMatchesUsernames (this also removes duplicates)
For $i In $OldFoundMatchesUsernames
    $sdFoundMatchesUsernames.Item($i)
Next
 ; get new $FoundMatchesUsernames array
$FoundMatchesUsernames = $sdFoundMatchesUsernames.Keys()
_ArraySort($FoundMatchesUsernames)
_ArrayDisplay($FoundMatchesUsernames, "$FoundMatchesUsernames")

Edit
Comments added

Edited by mikell
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...