Jump to content

Recommended Posts

Posted

Hello,

i was trying to delete some arrays

but it is not working for me

help is requested

#include <Array.au3>

local $year = [2010, 2007, 2014, 2013]

For $x = 0 To UBound($year) - 1
    $yearNow = "2018"
    $Compare = StringCompare($year, $yearNow)
    ConsoleWrite($Compare & @CRLF)
    If $Compare < 0 Then
        _ArrayDelete($year, $x)
    EndIf
Next

_ArrayDisplay($year)

try this code, and you will see what i am getting

i tried to change the years, but i am always left with 1 or 2

thanks in advance

Posted

Ttamari,

As you delete rows you are no longer bumping through the array sequentially.

Delete them in reverse order like

for $x = ubound($array) - 1 to 0

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Posted
  On 3/21/2018 at 1:50 AM, kylomas said:

first, let me thank you for your answer

for $x = ubound($array) - 1 to 0

do you mean like this

#include <Array.au3>

local $year = [2010, 2007, 2014, 2013]

For $x = UBound($year) - 1 to 0
    $yearNow = "2018"
    $Compare = StringCompare($year, $yearNow)
    ConsoleWrite($Compare & @CRLF)
    If $Compare < 0 Then
        _ArrayDelete($year, $x)
    EndIf
Next

_ArrayDisplay($year)

i am afraid it is not working for me

i did try this second code, but the same result

#include <Array.au3>

local $year = [2010, 2007, 2014, 2013]

Local $deletearray[0]

For $x = 0 To UBound($year) - 1
    $yearNow = "2018"
    $Compare = StringCompare($year, $yearNow)
    ConsoleWrite($Compare & @CRLF)
    If $Compare < 0 Then
        _ArrayAdd($deletearray, $x)
    EndIf
Next

_ArrayDisplay($deletearray, "arraystodelete")

For $y = 0 To UBound($deletearray) - 1
    _ArrayDelete($year, $y)
Next

_ArrayDisplay($year)

 

Expand  

 

Posted

i think you need to "step -1" at the end of your For statement (if you are going to count backward you have to tell it)

  Reveal hidden contents

Posted

Look up For...To...Step...Next in the AutoIt help file and notice "Step -1" is referring to the optional parameter stepval as in "[Step < numeric step value>]".   The numeric step value being minus 1.

My example 1 is the more traditional, faster method that all the helpers of this thread are referring to.

#include <Array.au3>

Local $aYear = [2019, 2007, 2014, 2018, 2013]
Local $aYearNow = "2018" ; @YEAR ; This line does not need to be declared in each loop in the For - Next loop.
Local $aYearA = $aYear ; Original $aYear array for use in both examples - Examples should return same answers.

; Example 1
For $x = UBound($aYearA) - 1 To 0 Step -1
    $Compare = StringCompare($aYearA[$x], $aYearNow) ; Compare each element (where $x is the index of the element) of the array - Not the entire array as in your example.
    ConsoleWrite($Compare & @CRLF) ; StringCompare returns:-   0 when string1 and string2 are equal:
    ;                                                          1 when string1 is greater than string2
    ;                                                         -1 when string1 is less than string2 (see AutoIt help file)
    If $Compare < 0 Then
        _ArrayDelete($aYearA, $x)
    EndIf
Next
_ArrayDisplay($aYearA)

ConsoleWrite("==== ConsoleWrites StringCompares in reverse order =====" & @CRLF)

; Example 2
For $Element In $aYear
    $Compare = StringCompare($Element, $aYearNow)
    ConsoleWrite($Compare & @CRLF)
    If $Compare < 0 Then
        _ArrayDelete($aYear, _ArraySearch($aYear, $Element)) ; _ArraySearch used because there is no array index readily available in this example.
    EndIf
Next
_ArrayDisplay($aYear)

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...