Jump to content
LoneWolf_2106

Remove empty elements in array - slowness problem

Recommended Posts

LoneWolf_2106

Hi,

I have an array with thousands empty elements, I have written a for loop to remove them, but it is very slow.

For $i=UBound($aFile_Array) -1 To 0 Step -1
    If $aFile_Array[$i] = "" Then _ArrayDelete($aFile_Array, $i)
Next

is there any way to improve the process?

Thanks in advance for your support.

Share this post


Link to post
Share on other sites
water

When processing the Array, can't you just ignore empty elements?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
mikell

The slowness is caused by the constant call to _ArrayDelete
Here is a possible way - for 1D arrays

#include <Array.au3>

Local $array[50000]
For $i = 0 To UBound($array)-1 step 2
    $array[$i] = "This is a test" & $i
Next
_ArrayDisplay($array)

Local $string
For $i = 0 To UBound($array)-1
   $string &= $array[$i] & @crlf
Next

$res = StringRegExp($string, '\V+', 3)
_ArrayDisplay($res)

 

Share this post


Link to post
Share on other sites
LarsJ
$nFile_Array = UBound($aFile_Array)
Dim $aTmp[$nFile_Array]
$iFile_Array = 0
For $i = 0 To $nFile_Array - 1
  If Not $aFile_Array[$i] Then ContinueLoop
  $aTmp[$iFile_Array] = $aFile_Array[$i]
  $iFile_Array += 1
Next
ReDim $aTmp[$iFile_Array]
$aFile_Array = $aTmp

 

  • Like 1

Share this post


Link to post
Share on other sites
AutoBert

I think fastest solution is to sort the array in descending order, get the lowest value and then ReDim:

#include <Array.au3>

Dim $aArray[10000]

For $i=0 To 100
    $aArray[$i]=$i
Next

_ArrayShuffle($aArray)
_arraydisplay($aArray,'Array with many empty elements')

$tdStart=TimerInit()
_ArraySort($aArray,1)
$iMin= _ArrayMinIndex($aArray)
ReDim $aArray[$iMin]
$tdDiff=TimerDiff($tdStart)
_ArrayDisplay($aArray,'TimerDiff = '&$tdDiff)

 

Share this post


Link to post
Share on other sites
mikell
2 hours ago, AutoBert said:

I think fastest solution is ...

Did you try a little comparison ?    :whistle:

Share this post


Link to post
Share on other sites
LoneWolf_2106

I am trying to rewrite the structure of my software to reduce the number of those empty elements, I will let you know as soon as I have finished

Share this post


Link to post
Share on other sites
LoneWolf_2106
Posted (edited)

I don't know if I have to open a new topic, anyway I ask you here. Is it possible to remove a XML node and its content from a file?

 

I have tried this, but not all the values are removed, most likely because the indexes are not updated(found with _ArrayFindAll) during the the deletion: 

 

$test_start=_ArrayFindAll($Array_Test, "<test>",0,0,0,1)

$test_End=_ArrayFindAll($Array_Test, "</test>",0,0,0,1)

For $i=0 to UBound($test_start) -1


    For $j=$test_start[$i] To $test_End[$i]
        _ArrayDelete($Array_Test, $j)
    Next


Next

Edit: The node is on multiple lines

Edited by LoneWolf_2106

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

×