Renderer Posted December 29, 2019 Posted December 29, 2019 Hi there! I have got a small problem regarding arrays. I try to selectively delete some elements of an array. To do that, I create a new array, that contains the indexes of the array, from which they should be deleted. Bellow I've posted some code: global $iArray[5] = ["H","E","L","L","O"] global $index[3] = [2,3,4] ; Contains the Indexes of $iArray that should be deleted _ArrayDisplay($iArray, "Array Before Delete") ;First Attempt: _ArrayDelete($iArray, $index) _ArrayDisplay($iArray) ; Failed ;Second Attempt: for $i = 0 to UBound($index) -1 _ArrayDelete($iArray, $index[$i]) Next _ArrayDisplay($iArray); Failed The question is: How do I get to remove the indexes 2,3,4 contained by ($index array) from the main array ($iArray)? Thanks in advance!
Nine Posted December 29, 2019 Posted December 29, 2019 The array passed to _ArrayDelete must be 1 based with count in 0. #include <Array.au3> local $iArray[5] = ["H","E","L","L","O"] Local $index[4] = [3,2,3,4] ; Contains the Indexes of $iArray that should be deleted _ArrayDisplay($iArray, "Array Before Delete") _ArrayDelete($iArray, $index) _ArrayDisplay($iArray) ; Don't fail anymore “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Subz Posted December 29, 2019 Posted December 29, 2019 Also remember when deleting within a loop (using +1) to sort your [indexes array] from largest to smallest, _ArraySort($index, 1) otherwise you will get unexpected results as the original arrays bounds are also changed when modifying the array, for example: global $iArray[5] = ["A","B","C","D","E"] global $index[3] = [2,3,4] ; Contains the Indexes of $iArray that should be deleted _ArrayDelete($iArray, $index[0]) ;~ Deletes 2nd index "C" ;~ Result: $iArray[4] = ["A","B","D","E"] _ArrayDelete($iArray, $index[1]) ;~ Deletes 3rd index "E" ;~ Result: $iArray[3] = ["A","B","D"] _ArrayDelete($iArray, $index[2]) ;~ Error 5 - $vRange content is outside array bounds ;~ Result: $iArray[3] = ["A","B","D"]
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