Sign in to follow this  
Followers 0
Stoex

deleting elements using unbound

9 posts in this topic

I saw this code on the site and have attempted to modify it so I could delete elements in a 2D array if a given column was 0.

I keep running into errors, so could somebody please let me know how it would change.

Thanks.

#include <Constants.au3>
#include <Array.au3>

Local $aArray[4][3] = [["col 0", "col1", "col2"], ["abc", "efg", "123"], ["", "", ""], ["hij", "", "xyz"]]
Local $iCount = 0, $sTemp
_ArrayDisplay($aArray)
For $i = 0 To UBound($aArray, 1) - 1
   $sTemp = ""
   For $j = 0 To UBound($aArray, 2) - 1
      $aArray[ $iCount][$j] = $aArray[$i][$j]
      $sTemp &= $aArray[$i][$j]
   Next

   If $sTemp <> "" Then  $iCount += 1
Next

Share this post


Link to post
Share on other sites



how about (not sure if this is what you need)

#include <Constants.au3>
#include <Array.au3>

Local $aArray[6][3] = [["col 0", "col1", "col2"], ["", "", ""], ["", "", ""], ["abc", "efg", "123"], ["", "", ""], ["hij", "", "xyz"]]
Local $iCount = 0, $sTemp
_ArrayDisplay($aArray)
For $i = UBound($aArray) - 1 To 0 Step -1
    $bRemove = True
    For $j = 0 To UBound($aArray,2) - 1
        If StringLen($aArray[$i][$j]) > 0 Then
            $bRemove = False
            ExitLoop
        EndIf
    Next
    If $bRemove Then
        For $x = $i To UBound($aArray) - 1
            If $x < UBound($aArray) - 1 Then
                For $j = 0 To UBound($aArray,2) - 1
                    $aArray[$x][$j] = $aArray[$x+1][$j]
                Next
            EndIf
        Next
        ReDim $aArray[UBound($aArray)-1][UBound($aArray,2)]
    EndIf
Next
_ArrayDisplay($aArray)

It's removing empty rows, but you can modify to only remove if some column is empty


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
jguinch

I couldn't remember who the code was written by, but looks like its you.

You first code (above) was great in assisting me to learn more about this array stuff. My goal then was to delete elements where a blank occurred.  I then looked to delete elements based upon a 0 (zero) instead of a blank.  I tried to simply modify what you originally coded but cannot get it to work and thus I go in circles. So now in a 2D array I'm looking to delete a "row" if a 0 shows up in a column in that row.

For instance in your code: If $sTemp <> "" Then  $iCount += 1

I just replaced the "" with a zero, but its not working. Looked to me as if it doesn't equal 0, keep it. Guess I'm missing something.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Something like this ?

#include <Array.au3>
Local $aArray[4][3] = [["col 0", "col1", "col2"], ["abc", "efg", 0], ["abc", 0, "def"], ["hij", "klm", "nop"]]
Local $iCount = 0, $sTemp

For $i = 0 To UBound($aArray, 1) - 1
    $iDelete = 0
    
    For $j = 0 To UBound($aArray, 2) - 1
        $aArray[ $iCount][$j] = $aArray[$i][$j]
        If $aArray[$i][$j] == 0 OR $aArray[$i][$j] = "" Then $iDelete = 1
    Next
    If NOT $iDelete  Then  $iCount += 1
Next

Redim $aArray[ $iCount][UBound($aArray, 2) ]

_ArrayDisplay($aArray)
Edited by jguinch
1 person likes this

Share this post


Link to post
Share on other sites
jguinch

that seems similar to your first one, in which I was looking to remove blanks.

The goal was to read the array and delete rows where a column could contain a blank or a 0.

I just tried using both your codes, one right after the other - first remove blanks, then remove 0's.

I have something wrong since the 0's are deleted but the blanks remain.

Share this post


Link to post
Share on other sites
jdelaney

i tried your code after modifying it and it worked, I also ran it 1. to delete blanks and 2. to delete 0's.

It works. Not saying I fully get it, since I am new at all this.  

Share this post


Link to post
Share on other sites
jguinch

Nice!

Hits both.  For me, as a beginner, I find shorter execution routes allow me to understand it better. Otherwise I can get lost.

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  
Followers 0