Hobbyist Posted September 16, 2014 Share Posted September 16, 2014 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 expandcollapse popup#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") Link to comment Share on other sites More sharing options...
jguinch Posted September 16, 2014 Share Posted September 16, 2014 (edited) I think there are some functions on the forum for this... Here is my own function, maybe it can help you : expandcollapse popup#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 September 16, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 16, 2014 Moderators Share Posted September 16, 2014 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jguinch Posted September 16, 2014 Share Posted September 16, 2014 @melba : easier, easier... I did not use Array.au3 However, I did not test the 0 values as blanks... Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Luigi Posted September 16, 2014 Share Posted September 16, 2014 (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 September 16, 2014 by Detefon Visit my repository Link to comment Share on other sites More sharing options...
Hobbyist Posted September 16, 2014 Author Share Posted September 16, 2014 @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, ). 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 Link to comment Share on other sites More sharing options...
jguinch Posted September 16, 2014 Share Posted September 16, 2014 (edited) Oups, sorry. I made an error in my code. I just edit it. Can you try again ? For information, _ArrayDelete performs a Redim to. Edited September 16, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
BrewManNH Posted September 16, 2014 Share Posted September 16, 2014 _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 GudeHow 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 Link to comment Share on other sites More sharing options...
mikell Posted September 16, 2014 Share Posted September 16, 2014 (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 September 16, 2014 by mikell Link to comment Share on other sites More sharing options...
Hobbyist Posted September 16, 2014 Author Share Posted September 16, 2014 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) Link to comment Share on other sites More sharing options...
Hobbyist Posted September 16, 2014 Author Share Posted September 16, 2014 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now