Sign in to follow this  
Followers 0
Hobbyist

Delete items from Array - two examples

11 posts in this topic

I'm looking at two different approaches thought to be used in deleting "blanks" or "zeros" from an array.

The second method works (and its just for blanks) while the first method for blanks AND zeros a result that is NOT good.

You have to uncomment/comment each method, but could someone please point out why one is not working - what needs to be tweaked and corrected?  

Thanks.

Hobbyist

#include <Constants.au3>
#include <Array.au3>

Global $aLogBUD2[6][3] = [["","",""],["col 0", "col1", "col2"], ["abc", "efg", "123"],["","",""], ["", "", ""], ["hij", "", "xyz"]]

_ArrayDisplay($aLogBUD2, "initial array")

; METHOD ONE FOR DELETE 0 OR BLANKS *****************
Local $iCount = 0, $sTemp
For $i = 0 To UBound( $aLogBUD2, 1)  - 1
    $iDelete = 0

    For $j = 0 To UBound( $aLogBUD2, 2) - 1
        $aLogBUD2[ $iCount][$j] =  $aLogBUD2[$i][$j]
        If  $aLogBUD2[$i][$j] == 0 OR $aLogBUD2[$i][$j] = ""   Then $iDelete = 1

    Next
    If NOT $iDelete  Then  $iCount += 1
Next
;END METHOD ONE

;METHOD TWO FOR DELETE BLANKS
;~ For $i = UBound($aLogBUD2) - 1 To 0 Step -1
;~     ; Create a blank variable
;~     $sTemp = ""
;~     For $j = 0 To UBound($aLogBUD2, 2) - 1
;~         ; Add each of the elements in the row
;~         $sTemp &= $aLogBUD2[$i][$j]
;~         ; If it contains something then ignore this row
;~         If $sTemp <> "" Then ContinueLoop 2
;~     Next
;~     ; There was nothing at all in that row
;~     _ArrayDelete($aLogBUD2, $i)
;~ Next



;END METHOD TWO

_ArrayDisplay($aLogBUD2, "ending")

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think there are some functions on the forum for this...

Here is my own function, maybe it can help you :

#Include <Array.au3> ; Just for _ArrayDisplay
 
 
Global $aLogBUD2[6][3] = [["","",""],["col 0", "col1", "col2"], ["abc", "efg", "123"],["","",""], ["", "", ""], ["hij", "", "xyz"]]
_ArrayDeleteBlankLines2D($aLogBUD2)
_ArrayDisplay($aLogBUD2)
 

 
 
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayDeleteBlankLines2D
; Description ...: Delete all blank lines in a 2D array
; Syntax ........: _ArrayDeleteBlankLines2D(Byref $aArray)
; Parameters ....: $aArray              - [in/out] An array of unknowns.
; Return values .: Sucess : the new size of the array
;                  Failure : -1 and sets the @error flag to non-zero
;                    @error : 1 - $aArray is not an array
;                             2 - $aArray is not a 2D array
; Author ........: JGUINCH
; ===============================================================================================================================
Func _ArrayDeleteBlankLines2D(ByRef $aArray)
    Local $iIndex = 0, $sData
 
    If NOT IsArray($aArray) Then Return SetError(1, 0, -1)
    IF UBound($aArray, 0) <> 2 Then Return SetError(2, 0, -1)
 
    For $i = 0 To UBound($aArray) - 1
        $sData = ""
        For $j = 0 To UBound($aArray, 2) - 1
            $sData &= $aArray[$i][$j]
            $aArray[$iIndex][$j] = $aArray[$i][$j]
        Next
        ; Added Melba's regexp test :)
        ; If NOT StringRegExp($sData, "^0{0,3}$") Then $iIndex += 1

        ; More that one 0 in the same cell
        If NOT StringRegExp($sData, "^0*$") Then $iIndex += 1
    Next
 
    Redim $aArray[$iIndex][ UBound($aArray, 2) ]
    Return UBound($aArray)
 
EndFunc ; ===> _ArrayDeleteBlankLines2D
Edited by jguinch

Share this post


Link to post
Share on other sites

Hobbyist,

I think this is an easier algorithm: :)

#include <Constants.au3>
#include <Array.au3>

Global $aLogBUD2[6][3] = [["","",""],["col 0", "col1", "col2"], ["abc", "efg", "123"],["","0",""], ["0", "", "0"], ["hij", "", "xyz"]]

_ArrayDisplay($aLogBUD2, "initial array")

; When deleting lines always start from the bottom
For $i = UBound($aLogBUD2) - 1 To 0 Step -1
    ; Get the content of the line
    $sLine = ""
    For $j = 0 To UBound($aLogBUD2, 2) - 1
        $sLine &= $aLogBUD2[$i][$j]
    Next
    
    ; If the line can be empty or only containing 1, 2 or 3 "0"s
    If StringRegExp($sLine, "^0{0,3}$") Then                        ; <<<<<<<<<<<< This line

    ; Or if the line must be completely empty
    ;If $sLine = "" Then                                            ; <<<<<<<<<<<< Or this one
    
        ; Delete the line
        _ArrayDelete($aLogBUD2, $i)
    EndIf
Next

_ArrayDisplay($aLogBUD2, "ending")
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I never have luck with _ArrayDelete "on-the-fly", or delete a array element inside loop.

I always use a select elements to delete and add in a secondary array, then finish the main loop, delete all selected elements

In this example I search all odd/unpaired number and add in aDel

See...

#include <Array.au3>

Global $arr[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Global $aDel[1]

For $ii = 0 To UBound($arr, 1) - 1
    ConsoleWrite($arr[$ii] & @LF)
    If Mod($arr[$ii], 2) Then _ArrayAdd($aDel, $ii)
Next

For $ii = 1 To UBound($aDel, 1) - 1
    _ArrayDelete($arr, $ii)
Next
_ArrayDisplay($arr)
Edited by Detefon

m(o.O)m

Share this post


Link to post
Share on other sites

@Melba, @ jguinch

Thanks for the insight. I can follow both examples.

I can see Melba's starts at the end of the array and works to the beginning, while jguinch's starts at the beginning and then needs to redim.  I can definitely put these into my bag of code.

Additionally, in the code I attached there are also both starting points - one at the end and one at the beginning.  The code which starts at the end of the array does work though different than Melba's (now I have two,  :shifty: ).  The second code which starts at the beginning, likes jguinch's, does NOT work well and yet I don't really know why.  I did not redim, as jguinch does, but only so I could see what was happening - thus I can see dups (not good).

Any thoughts?  And thanks again.

Hobbyist

Share this post


Link to post
Share on other sites

_ArrayDelete is also extremely slow on large arrays when deleting items one at a time in a loop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Melba,

Your code fails if an element is like this : ["0", "00", "0"] or like this : ["","","   "]

But this one works

#include <Array.au3>

Global $aLogBUD2[6][3] = [["","  ",""],["col 0", "col1", "col2"], ["abc", "efg", "123"],["","0",""], ["0", "00", "0"], ["hij", "", "xyz"]]

_ArrayDisplay($aLogBUD2, "initial array")

For $i = UBound($aLogBUD2) - 1 To 0 Step -1
    $sLineValue = 0
    For $j = 0 To UBound($aLogBUD2, 2) - 1
        $sLineValue += StringRegExp($aLogBUD2[$i][$j], '[^0\s]+')
    Next
    If $sLineValue = 0 Then _ArrayDelete($aLogBUD2, $i)
Next

_ArrayDisplay($aLogBUD2, "ending")

Edit

meaning : "line must contain one or more char which is not a 0 or a white space"  :)

Edited by mikell

Share this post


Link to post
Share on other sites
jguinch

 

Tried your new version. Thanks.

I also commented out your redim to see what happens and its results are similar to what I was getting in method one. And I thought that was wrong - but did not know Array Delete does a redim - now I have better knowledge of it. Thanks.

(so seeing the last line before redim was not an error just incomplete processing -Whew)

Share this post


Link to post
Share on other sites
mikell

 

Good points. And i come away with still better knowledge of how to think this stuff through.

 

And thanks to all - Melba, jguinch and mikell

 

Hobbyist

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