Dgameman1

My code isn't deleting strings from an Array?

11 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



Maybe there are whitespaces in the arraystrings, you can use StringStripWS to strip them. Can you please post some demodata for $MatchesHTML or $FoundMatchesUsernames?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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
1 person likes this

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

 

Edited by mikell
1 person likes this

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
For $i = UBound($FoundMatchesUsernames) - 1 To 0 Step -1   ;<< loop to 0

:)

1 person likes this

Share this post


Link to post
Share on other sites
4 hours ago, mikell said:
For $i = UBound($FoundMatchesUsernames) - 1 To 0 Step -1   ;<< loop to 0

:)

You're the best!

And I'm assuming I should just go ahead and change it for both then. Thanks so much

Share this post


Link to post
Share on other sites

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

1 person likes this

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites
5 hours ago, BetaLeaf said:

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

You're 100% correct. Thanks :)

1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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
2 people like this

Share this post


Link to post
Share on other sites

Damn @mikell I would've never thought about it like that. Good looking out

1 person likes this

Share this post


Link to post
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