Jump to content

Array delete blank element


Recommended Posts

Hi,

i have the following array with 2 blank elements:

dim $arr1[4]

$arr1[0]=""

$arr1[1]="ABC"

$arr1[2]=""

$arr1[3]="xyz"

i want now to delete the blank elements and put the result in a new Array --> this mean i will get in this case just two elemet in $arr2 and the length of the new array ($arr2) will be two:

$arr2[0]="ABC"

$arr2[1]="xyz"

any idea?

Thanks

Edited by marshallprank
Link to comment
Share on other sites

  • Moderators

marshallprank,

Easy (when you know how)! :mellow:

#include <Array.au3>

Global $Content_Subfolder_include[3] = ["", "ABC", ""]

; Copy the array
$arr_2 = $Content_Subfolder_include

; Move backwards through the array deleting the blank lines
For $i = UBound($arr_2) - 1 To 0 Step -1
    If $arr_2[$i] = "" Then
        _ArrayDelete($arr_2, $i)
    EndIf
Next

; Et voila!
_ArrayDisplay($arr_2)

Note you need to move backwards through the array when deleting lines or you end up with element indexing errors. :)

All clear? :)

M23

Edit: And it still works if you add an element to the initial array as you have just done in your edit. But do rermember to increase the dimension size of the initial array if you do that. :)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Something like this ?

#include <Array.au3>

dim $arr1[4]
$arr1[0]=""
$arr1[1]="ABC"
$arr1[2]=""
$arr1[3]="xyz"


$arr2 = _RemoveEmptyArrayElements ( $arr1 )
_ArrayDisplay ( $arr2 )

Func _RemoveEmptyArrayElements ( $_Array )
    Local $_Item
    For $_Element In $_Array
        If $_Element= '' Then
            _ArrayDelete ( $_Array, $_Item )
        Else
            $_Item+=1
        EndIf
    Next
    Return ( $_Array )
EndFunc ;==> _RemoveEmptyArrayElements ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

  • Moderators

Hannes123,

Nice piece of lateral thinking there! :mellow:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

If you have big arrays I suggest you to do the following:

#include <array.au3>

dim $arr1[4]
$arr1[0]=""
$arr1[1]="ABC"
$arr1[2]=""
$arr1[3]="xyz"

$s_array = _ArrayToString($arr1, "|")
$s_array = StringReplace($s_array, "||", "|")

$arr2 = StringSplit($s_array, "|", 2)

It is much faster than _ArrayDelete() because you don't have to redim the array all the time. :mellow:

thanks for this :)

:alien: ~ Every Living Thing is a Code Snippet of World Application ~ :alien:

Link to comment
Share on other sites

An example of how to remove blanks using only the original array.

Func _ArryRemoveBlanks(ByRef $arr)
  $idx = 0
  For $i = 0 To UBound($arr) - 1
    If $arr[$i] <> "" Then
      $arr[$idx] = $arr[$i]
      $idx += 1
    EndIf
  Next
  ReDim $arr[$idx]
EndFunc ;==>_ArryRemoveBlanks

;Some code just to show it working
#include <array.au3>
Dim $arr1[10]
$arr1[0] = ""
$arr1[1] = "ABC"
$arr1[2] = ""
$arr1[3] = "xyz"
$arr1[4] = "def"
$arr1[5] = ""
$arr1[6] = ""
$arr1[7] = "ffr"
$arr1[8] = ""
$arr1[9] = "Z33"

_ArrayDisplay($arr1, "Before")
_ArryRemoveBlanks($arr1)
_ArrayDisplay($arr1, "After")

Nice Method i like this one
Link to comment
Share on other sites

  • 7 months later...

If you have big arrays I suggest you to do the following:

#include <array.au3>

dim $arr1[4]
$arr1[0]=""
$arr1[1]="ABC"
$arr1[2]=""
$arr1[3]="xyz"

$s_array = _ArrayToString($arr1, "|")
$s_array = StringReplace($s_array, "||", "|")

$arr2 = StringSplit($s_array, "|", 2)

It is much faster than _ArrayDelete() because you don't have to redim the array all the time. :)

Have you tried it yet?In fact, it's slowest method when convert to string and redim new array ;))
Link to comment
Share on other sites

Oblivionx,

Have you seen the date of that post? Try not to make a habit of necroing threads, especially when you have nothing useful to contribute. Thanks.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 4 years later...
  • 2 months later...

I think you should have gone back further to Hannes answer and then have some nested fun

#include <array.au3>

dim $arr1[5]
$arr1[0]=""
$arr1[1]="ABC"
$arr1[2]=""
$arr1[3]="xyz"
$arr1[4]=""

_ArrayDisplay(StringSplit(StringRegExpReplace(StringRegExpReplace(_ArrayToString($arr1, "|"), "(\A\|)|(\|\z)", "") , "(\|\|)" , "|") , "|" , 2))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Necropost.png

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

Link to comment
Share on other sites

Because the forums are a veritable hotbed of activity today this will throw everything off, what were we thinking.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • 2 years later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...