Jump to content
kcvinu

How to delete an element from a 1D array inside a loop

Recommended Posts

Hi all,

I am trying to delete a specific element from a 1D array. But it doesn't delete the specific item. Here is my code

Local $Items_InFolder
Local $element
Local $Search_String
Local $Comparison

$Items_InFolder= _FileListToArray(@ScriptDir)
$Search_String = "- AutoIt Forums_files"

For $element In $Items_InFolder
    $Comparison = StringInStr($element,$Search_String)
    If $Comparison < 1 Then
        _ArrayDelete($Items_InFolder,$element)
        
    EndIf
Next

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Need to go backwards when deleting array elements.

#include <File.au3>
#include <Array.au3>

Local $Items_InFolder

Local $element

Local $Search_String

Local $Comparison



$Items_InFolder= _FileListToArray(@ScriptDir)

_ArrayDisplay($Items_InFolder)

$Search_String = "- AutoIt Forums_files"



For $element = $Items_InFolder[0] To 1 Step -1

    $Comparison = StringInStr($Items_InFolder[$element],$Search_String); EDIT: oops StringInStr($element,$Search_String)

    If $Comparison < 1 Then

        _ArrayDelete($Items_InFolder,$element)
        $Items_InFolder[0] -= 1

    EndIf

Next

_ArrayDisplay($Items_InFolder)
Edited by JohnOne

Share this post


Link to post
Share on other sites

BTW you should avoid the use of For/in/Next loops (_Array* funcs mainly work using indexes )

Example

#include <Array.au3>

Local $aArray[5] = ["test0", "test2", "test4", "test6", "test8"]
;_ArrayDisplay($aArray, "Original")
For $vElement In $aArray
      If StringInStr($vElement, "6") Then 
          Msgbox(0,"", $vElement)
          _ArrayDelete($aArray, $vElement)  ; <== doesnt work
     EndIf
Next
_ArrayDisplay($aArray, "Element deleted")

Share this post


Link to post
Share on other sites

@

JohnOne I think ArrayName[0] is the first element of the array. Then how can you iterate from start to end with a minus step value ?

Edit: You are iterating from 0th element to 1st element ?. 

Edited by kcvinu

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

$aArray[0] = Number of FilesFolders returned

https://www.autoitscript.com/autoit3/docs/libfunctions/_FileListToArray.htm

Edited by JohnOne

Share this post


Link to post
Share on other sites

Ok...Ok.. Now i  got it. :)


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Thank you 

JohnOne, Solved. I wanted to delete a file named "lightbox.JS" But there are plenty of them. So i made a script to delete it. And it will do the job in 1 second. There are 49 files in 49 folders. Thanks to autoit. 

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

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

×
×
  • Create New...