Jump to content
Sign in to follow this  
litlmike

ReDim, Remove @CRLF & WS from Array

Recommended Posts

litlmike

I have an Array, that may or may not have trailing @CRLF that may occupy an entire element in an array. I would like to remove the @CRLF and resize the array. The issue I am having is that my 'commands' are nested inside a loop and the loop errors out because "Array variable has incorrect number of subscripts or subscript dimension range exceeded", which is accurate. I assume there is a commonly used technique to correct this, that I am probably just not aware of. Or, I am just missing something entirely. Thanks for the help

#include<Array.au3>


Local $aCategories[5] = ["String", "Space" , "Hello World!" , @CRLF, @CRLF]
;Strip all the @CRLFs from the array
    For $i=0 To UBound ($aCategories)-1
        $aCategories[$i] = StringStripWS ( $aCategories[$i], 3 ) ;Remove the @CRLFs
        If $aCategories[$i]="" Then ;If any element is blank, then ReDim Array
            _ArrayDelete($aCategories, $i)
            ReDim $aCategories[UBound ($aCategories)]
            If $i >= UBound($aCategories) Then ExitLoop
        EndIf
        
    Next
_ArrayDisplay ($aCategories)

Share this post


Link to post
Share on other sites
weaponx

Typical programming mistake. Your loop iterates through an array from a predefined start to a predefined end. Deleting an element and redimensioning the array will not tell your loop that the array has gotten smaller.

Lets say you have a 3 element array:

Dim $array[3] = ["A","B","C"]

$NumberOfElements = Ubound($array)

For $X = 0 to $NumberOfElements - 1

If $array[$X] = "B" Then _ArrayDelete($array, $X)

Next

As soon as the condition is met your array will suddenly have only 2 elements but your loop will make an attempt to access that third element, Resulting in failure.

You need to rethink your logic. Maybe instead of deleting elements, blank them out. Also you could just create a second array by copying only the "good" elements.

Share this post


Link to post
Share on other sites
PsaltyDS

I have an Array, that may or may not have trailing @CRLF that may occupy an entire element in an array. I would like to remove the @CRLF and resize the array. The issue I am having is that my 'commands' are nested inside a loop and the loop errors out because "Array variable has incorrect number of subscripts or subscript dimension range exceeded", which is accurate. I assume there is a commonly used technique to correct this, that I am probably just not aware of. Or, I am just missing something entirely. Thanks for the help

#include<Array.au3>


Local $aCategories[5] = ["String", "Space" , "Hello World!" , @CRLF, @CRLF]
;Strip all the @CRLFs from the array
    For $i=0 To UBound ($aCategories)-1
        $aCategories[$i] = StringStripWS ( $aCategories[$i], 3 ) ;Remove the @CRLFs
        If $aCategories[$i]="" Then ;If any element is blank, then ReDim Array
            _ArrayDelete($aCategories, $i)
            ReDim $aCategories[UBound ($aCategories)]
            If $i >= UBound($aCategories) Then ExitLoop
        EndIf
        
    Next
_ArrayDisplay ($aCategories)
Oh, c'mon litlmike! I know you've seen this one before! Walk the array in reverse with:
For $i = UBound($aCategories)-1 To 0 Step -1

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
litlmike

Oh, c'mon litlmike! I know you've seen this one before! Walk the array in reverse with:

For $i = UBound($aCategories)-1 To 0 Step -1

:)

Dang it, You are right! ... I have seen this before. Here is what would be helpful, when do I use a normal loop and when do I use this 'diminishing' loop? Frankly, it makes my brain hurt to think in the reverse since I can barely think in the forward.. haha. But, if I can at least better identify which situations call for each type of looping, it will make my brain hurt less. :(

Also, why don't I need the ReDim in this method?

@WeaponX

Thanks for that walkthrough in the logic, it was helpful to understand what was going on and why

Share this post


Link to post
Share on other sites
weaponx

_ArrayDelete() calls ReDim for you.

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.