ZipleR Posted September 23, 2015 Share Posted September 23, 2015 Every time I need loop through an array and remove elements I run into issues.. Can anyone help me understand a good way to loop through an array, remove elements and NOT crash due to==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:Here is some sample code that I have to show you the type of loop I am talking about.Local $bResult[1], $aResult[6] = ["5","Item1","Item2","Item3","Item4","*Item5"] ;Loop through an array and remove any element starting with "*" For $x = 1 to $aResult[0] ;starting $x at 1 to skip the Array Size row [0]. If StringLeft($aResult[$x],1) = "*" then ; This will test if item $x in the array starts with "*" $bResult[0] = _ArrayAdd($bResult,$aResult[$x]) ;if it does, Add it to a second array and store the Array size in $bResult[0] $aResult[0] = _ArrayDelete($aResult,$x);remove the element from the array and store the new array size in $aResult[0] $x -= 1 ;This will lower $x by one so the next time through the element that moved up will be tested EndIf Next _ArrayDisplay($aResult, "Source") ;Display what was in the source array _ArrayDisplay($bResult, "Destination") ;Display what was moved to the destination array.Any advice would be much appreciated.Thank you! Link to comment Share on other sites More sharing options...
BrewManNH Posted September 23, 2015 Share Posted September 23, 2015 You need to loop through your array in reverse, otherwise you'll end up deleting the wrong elements, or your loop will run past the end of the array.#include <Array.au3> Local $bResult[1], $aResult[6] = ["5", "Item1", "*Item2", "Item3", "Item4", "*Item5"] ;Loop through an array and remove any element starting with "*" For $x = $aResult[0] To 1 Step - 1 ; < << <<< Loop through the array in reverse If StringLeft($aResult[$x], 1) = "*" Then ; This will test if item $x in the array starts with "*" $bResult[0] = _ArrayAdd($bResult, $aResult[$x]) ; if it does, Add it to a second array and store the Array size in $bResult[0] $aResult[0] = _ArrayDelete($aResult, $x); remove the element from the array and store the new array size in $aResult[0] EndIf Next _ArrayDisplay($aResult, "Source") ;Display what was in the source array _ArrayDisplay($bResult, "Destination") ;Display what was moved to the destination array. PoojaKrishna 1 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...
ZipleR Posted September 23, 2015 Author Share Posted September 23, 2015 So glad I asked.. That is WAY easier than I was making it...Thank you! 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