Sign in to follow this  
Followers 0
kor

How to delete lots of items from an array efficiently?

9 posts in this topic

Here is my current code.

Does anyone have any ideas how I might be able to clean this up?

If I could somehow delete items with keywords instead of having to name each item.

#include <ad.au3>
#include <array.au3>

_AD_Open()

Global $list[50]

$list[0] = "Account Operators"
$list[1] = "Administrators"
$list[2] = "Backup Operators"
$list[3] = "$F51000-LP7DJ67UIARD"
$list[4] = "Allowed RODC Password Replication Group"
$list[5] = "Cert Publishers"
$list[6] = "Certificate Service DCOM Access"
$list[7] = "Cryptographic Operators"
$list[8] = "Delegated Setup"
$list[9] = "Denied RODC Password Replication Group"
$list[10] = "DHCP Users"
$list[11] = "Discovery Management"
$list[12] = "Distributed COM Users"
$list[13] = "DnsAdmins"
$list[14] = "DnsUpdateProxy"
$list[15] = "Domain Admins"
$list[16] = "Domain Computers"
$list[17] = "Domain Controllers"
$list[18] = "Domain Guests"
$list[19] = "Domain Users"
$list[20] = "Enterprise Admins"
$list[21] = "Enterprise Read-only Domain Controllers"
$list[22] = "Event Log Readers"
$list[23] = "Exchange All Hosted Organizations"
$list[24] = "Exchange Servers"
$list[25] = "Exchange Trusted Subsystem"
$list[26] = "Exchange Windows Permissions"
$list[27] = "ExchangeLegacyInterop"
$list[28] = "Guests"
$list[29] = "Help Desk"
$list[30] = "Hygiene Management"
$list[31] = "IIS_IUSRS"
$list[32] = "Incoming Forest Trust Builders"
$list[33] = "Network Configuration Operators"
$list[34] = "Performance Log Users"
$list[35] = "Performance Monitor Users"
$list[36] = "Pre-Windows 2000 Compatible Access"
$list[37] = "Public Folder Management"
$list[38] = "RAS and IAS Servers"
$list[39] = "Read-only Domain Controllers"
$list[40] = "Recipient Management"
$list[41] = "Records Management"
$list[42] = "Replicator"
$list[43] = "Schema Admins"
$list[44] = "Server Management"
$list[45] = "Server Operators"
$list[46] = "Storage Servers"
$list[47] = "Terminal Server License Servers"
$list[48] = "UM Management"
$list[49] = "Users"

$var = _AD_GetObjectsInOU("", "(objectcategory=group)", 2)

_ArrayDisplay($var)

For $i = Ubound($var) - 1 to 0 Step - 1
    For $n = 0 to UBound($list) -1
        If StringInStr($var[$i], $list[$n]) then
            _ArrayDelete($var, $i)
        EndIf
    Next
Next

_ArrayDisplay($var)

_AD_Close()

I also notice that as soon my $list array gets over 52 items I get subscript errors.

Share this post


Link to post
Share on other sites



You could use _ArrayFindAll (which returns an array of indexes) then delete all these indexes (in reverse order as you did in your code).


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

I'm unsure how arrayfindall would help. i need to be able to delete multiple items based on expressions.

IE, delete all groups that have "servers" in it, delete all groups that start with $, etc.

Share this post


Link to post
Share on other sites

- find all elements containing "servers"

- delete returned indexes

- find all elements starting with "$"

- delete returned indexes

... and so on ...


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If the array records are very large, you could always read the array into an sqlite disk or memory db, perform a delete query based on your params, then read the sqlite db back into a new array.

Edit:

If you're list isn't really that big, then maybe this would be fast enough:

#include <ad.au3>
#include <array.au3>

_AD_Open()

Global $list[50]

$list[0] = "Account Operators"
$list[1] = "Administrators"
$list[2] = "Backup Operators"
$list[3] = "$F51000-LP7DJ67UIARD"
$list[4] = "Allowed RODC Password Replication Group"
$list[5] = "Cert Publishers"
$list[6] = "Certificate Service DCOM Access"
$list[7] = "Cryptographic Operators"
$list[8] = "Delegated Setup"
$list[9] = "Denied RODC Password Replication Group"
$list[10] = "DHCP Users"
$list[11] = "Discovery Management"
$list[12] = "Distributed COM Users"
$list[13] = "DnsAdmins"
$list[14] = "DnsUpdateProxy"
$list[15] = "Domain Admins"
$list[16] = "Domain Computers"
$list[17] = "Domain Controllers"
$list[18] = "Domain Guests"
$list[19] = "Domain Users"
$list[20] = "Enterprise Admins"
$list[21] = "Enterprise Read-only Domain Controllers"
$list[22] = "Event Log Readers"
$list[23] = "Exchange All Hosted Organizations"
$list[24] = "Exchange Servers"
$list[25] = "Exchange Trusted Subsystem"
$list[26] = "Exchange Windows Permissions"
$list[27] = "ExchangeLegacyInterop"
$list[28] = "Guests"
$list[29] = "Help Desk"
$list[30] = "Hygiene Management"
$list[31] = "IIS_IUSRS"
$list[32] = "Incoming Forest Trust Builders"
$list[33] = "Network Configuration Operators"
$list[34] = "Performance Log Users"
$list[35] = "Performance Monitor Users"
$list[36] = "Pre-Windows 2000 Compatible Access"
$list[37] = "Public Folder Management"
$list[38] = "RAS and IAS Servers"
$list[39] = "Read-only Domain Controllers"
$list[40] = "Recipient Management"
$list[41] = "Records Management"
$list[42] = "Replicator"
$list[43] = "Schema Admins"
$list[44] = "Server Management"
$list[45] = "Server Operators"
$list[46] = "Storage Servers"
$list[47] = "Terminal Server License Servers"
$list[48] = "UM Management"
$list[49] = "Users"

$var = _AD_GetObjectsInOU("", "(objectcategory=group)", 2)

_ArrayDisplay($var)

Global $ga_unique = __myAD_ArrayGetUnique($list, $var)

_ArrayDisplay($ga_unique)

_AD_Close()

Func __myAD_ArrayGetUnique($a_list, $a_objs)
    If Not IsArray($a_list) Or Not IsArray($a_objs) Then
        Return SetError(1, 0, 0)
    EndIf
    
    Local $i_ublist = UBound($a_list) - 1
    Local $i_ubobjs = UBound($a_objs) - 1
    Local $a_retarray[$i_ubobjs + 1], $i_add = 0
    
    For $iobj = 0 To $i_ubobjs
        For $ilist = 0 To $i_ublist
            If StringInStr($a_objs[$iobj], $a_list[$ilist]) Then
                ContinueLoop 2
            EndIf
        Next
        $a_retarray[$i_add] = $a_objs[$iobj]
        $i_add += 1
    Next
    
    If Not $i_add Then Return SetError(2, 0, 0) ; nothing unique
    ReDim $a_retarray[$i_add]
    
    Return $a_retarray
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

- find all elements containing "servers"

- delete returned indexes

- find all elements starting with "$"

- delete returned indexes

... and so on ...

Do you have an example of what this kind of code would look like?

Share this post


Link to post
Share on other sites

Note that the code in post #1 could fail because of this section

For $i = Ubound($var) - 1 to 0 Step - 1
    For $n = 0 to UBound($list) -1
        If StringInStr($var[$i], $list[$n]) then
            _ArrayDelete($var, $i)
        EndIf
    Next
Next

if there is a duplicate entry in the $var array then the inner loop will delete 2 elements, or, if a match is found in the last element then the $i index will be beyond the UBound of the i$var array. Maybe this would be safer

For $i = Ubound($var) - 1 to 0 Step - 1
    For $n = 0 to UBound($list) -1
        If StringInStr($var[$i], $list[$n]) then
            _ArrayDelete($var, $i)
            Exitloop;don't keep testing an element which has been deleted
                    ; and which might no longer exist,
                    ;and don't delete another element by mistake
        EndIf
    Next
Next

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

your second example works perfectly. Thank you.

Share this post


Link to post
Share on other sites

_ArrayDelete() constantly redim's the array every time it finds a match.

Thus my reason for producing an efficient means ( if the functions isn't what you wanted, you could at least see how to properly code your own version ) of gathering the data.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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
Sign in to follow this  
Followers 0