Sign in to follow this  
Followers 0
UmmDoughnuts

For Next Loop Values Stored ?

12 posts in this topic

OK... It's getting late here and I guess I'm going a little "Brain Dead"... :) I am using _FileReadToArray to create an array from a text file. This function stores the count of the number of records read into the [0] element of the array. This works fine.

Then, I want to do some "cleanup" using _ArrayDelete (when a record from the array meet a certain criteria) so I constructed a FOR NEXT loop as shown below... [simplified example...]

The "idea" was that after each _ArrayDelete the $aRecords[0] value would be "reduced" by one which would make the loop end "one iteration sooner" each time a row was deleted...

Dim $aRecords[1]
$aRecords[0] = 5
For $i = 1 to $aRecords[0]
   ConsoleWrite("--------------------------------------" & @CR)
   ConsoleWrite("$aRecords[0]: " & $aRecords[0] & @CR)
   ConsoleWrite("Count is: " & $i & @CR)
   If $i = 3 then 
     ;_ArrayDelete is used here...
      $aRecords[0] = $aRecords[0] - 1
      ConsoleWrite("----->$i: " & $i & "  $aRecords[0]: " & $aRecords[0] & @CR)
   EndIf
Next
ConsoleWrite("--------------------------------------" & @CR)
ConsoleWrite("*** END ***" & @CR)

The results look like this:

--------------------------------------
$aRecords[0]: 5
Count is: 1
--------------------------------------
$aRecords[0]: 5
Count is: 2
--------------------------------------
$aRecords[0]: 5
Count is: 3
----->$i: 3  $aRecords[0]: 4
--------------------------------------
$aRecords[0]: 4
Count is: 4
--------------------------------------
$aRecords[0]: 4
Count is: 5
--------------------------------------
*** END ***

As you can see, the loop continued through the 5th iteration even though the "TO" value of the loop had been reduced by 1 to 4...

Are the FOR / NEXT (/STEP) variable values somehow stored in alternate, local variables ??? Is there a way to "decrement" this value while the loop is in progress ?

</B>

Share this post


Link to post
Share on other sites



OK... It's getting late here and I guess I'm going a little "Brain Dead"... :) I am using _FileReadToArray to create an array from a text file. This function stores the count of the number of records read into the [0] element of the array. This works fine.

Then, I want to do some "cleanup" using _ArrayDelete (when a record from the array meet a certain criteria) so I constructed a FOR NEXT loop as shown below... [simplified example...]

The "idea" was that after each _ArrayDelete the $aRecords[0] value would be "reduced" by one which would make the loop end "one iteration sooner" each time a row was deleted...

Dim $aRecords[1]
$aRecords[0] = 5
For $i = 1 to $aRecords[0]
   ConsoleWrite("--------------------------------------" & @CR)
   ConsoleWrite("$aRecords[0]: " & $aRecords[0] & @CR)
   ConsoleWrite("Count is: " & $i & @CR)
   If $i = 3 then 
    ;_ArrayDelete is used here...
      $aRecords[0] = $aRecords[0] - 1
      ConsoleWrite("----->$i: " & $i & "  $aRecords[0]: " & $aRecords[0] & @CR)
   EndIf
Next
ConsoleWrite("--------------------------------------" & @CR)
ConsoleWrite("*** END ***" & @CR)

The results look like this:

--------------------------------------
$aRecords[0]: 5
Count is: 1
--------------------------------------
$aRecords[0]: 5
Count is: 2
--------------------------------------
$aRecords[0]: 5
Count is: 3
----->$i: 3  $aRecords[0]: 4
--------------------------------------
$aRecords[0]: 4
Count is: 4
--------------------------------------
$aRecords[0]: 4
Count is: 5
--------------------------------------
*** END ***

As you can see, the loop continued through the 5th iteration even though the "TO" value of the loop had been reduced by 1 to 4...

Are the FOR / NEXT (/STEP) variable values somehow stored in alternate, local variables ??? Is there a way to "decrement" this value while the loop is in progress ?

</B>

Instead of reducing the "To" Value,Increment the "For" one.

Share this post


Link to post
Share on other sites

The correct way to do this is walk the array in reverse. You only go through once, and deletions do not affect the indexes that have not been processed yet.

; Remove .bak files from the list
For $i = $aRecords[0] To 1 Step -1
    If StringRight($aRecords[0], 4) = ".bak" Then _ArrayDelete($aRecords, $i) 
Next
; Put the new count in [0]
$aRecords[0] = UBound($aRecords) - 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

The correct way to do this is walk the array in reverse. You only go through once, and deletions do not affect the indexes that have not been processed yet.

; Remove .bak files from the list
For $i = $aRecords[0] To 1 Step -1
    If StringRight($aRecords[0], 4) = ".bak" Then _ArrayDelete($aRecords, $i) 
Next
; Put the new count in [0]
$aRecords[0] = UBound($aRecords) - 1

:)

THANKS ! That's EXACTLY the answer I was looking for... :P

Does anyone know if there is a way to request that this "procedure" be added to the HELP FILE - perhaps in the "examples" section of _ArrayDelete ??? I'm sure having it there might save someone a LOT of time... ???

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

THANKS ! That's EXACTLY the answer I was looking for... :P

Does anyone know if there is a way to request that this "procedure" be added to the HELP FILE - perhaps in the "examples" section of _ArrayDelete ??? I'm sure having it there might save someone a LOT of time... ???

Dim $aRecords[1]
$aRecords[0] = 5
For $i = 1 to $aRecords[0]
   ConsoleWrite("--------------------------------------" & @CR)
   ConsoleWrite("$aRecords[0]: " & $aRecords[0] & @CR)
   ConsoleWrite("Count is: " & $i & @CR)
   If $i = 3 then 
  ;_ArrayDelete is used here...
      $aRecords[0] = $aRecords[0] - 1
      ConsoleWrite("----->$i: " & $i & "  $aRecords[0]: " & $aRecords[0] & @CR)
   EndIf
Next
ConsoleWrite("--------------------------------------" & @CR)
ConsoleWrite("*** END ***" & @CR)

Once your For...Next started, you cannot modify the value of $aRecords[0] by;

$aRecords[0] = $aRecords[0] - 1

Instead, you need to use;

$aRecords[0] -= 1

Or modify the value of $i;

If $i = 3 then 
  ;_ArrayDelete is used here...
      $i += 1
      ConsoleWrite("----->$i: " & $i & "  $aRecords[0]: " & $aRecords[0] & @CR)
   EndIf

Just letting you know that you were on the right track...PsaltyDS just makes it look good. :)

Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Share this post


Link to post
Share on other sites

Once your For...Next started, you cannot modify the value of $aRecords[0] by;

$aRecords[0] = $aRecords[0] - 1

Instead, you need to use;

$aRecords[0] -= 1

Just letting you know that you were on the right track...PsaltyDS just makes it look good. :)

Thanks for the information... Just curious... Why would $aRecords[0] -= 1 "work" when
$aRecords[0] = $aRecords[0] - 1 does not ???

Share this post


Link to post
Share on other sites

Thanks for the information... Just curious... Why would $aRecords[0] -= 1 "work" when

$aRecords[0] = $aRecords[0] - 1 does not ???
I don't know how to explain it but that's how it works.

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Share this post


Link to post
Share on other sites

... Why would $aRecords[0] -= 1 "work" when

$aRecords[0] = $aRecords[0] - 1 does not ???
There is no difference.  They are equivalent expressions.

:)


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

#9 ·  Posted (edited)

There is no difference. They are equivalent expressions.

:)

But they function differently inside a loop, I don't know why, but they do.

If you look at his console output, it loops all the way to 5 even if $aRecords[0] value is already changed to 4. Using "-=" changes the $aRecords[0] initial value, rather than giving it a new value.

I don't know if that makes sense. :/

EDIT: added some example

Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Share this post


Link to post
Share on other sites

But they function differently inside a loop, I don't know why, but they do.

If you look at his console output, it loops all the way to 5 even if $aRecords[0] value is already changed to 4. Using "-=" changes the $aRecords[0] initial value, rather than giving it a new value.

I don't know if that makes sense. :/

EDIT: added some example

You lost me. Can you modify this code to show any difference between the two?
Global $avData[5] = [4, 1, 2, 3, 4]
For $n = 1 To $avData[0]
    ConsoleWrite("$n = " & $avData[$n] & @LF)
    If $n = 3 then $avData[0] = $avData[0] - 1
Next

ConsoleWrite(@LF)

Global $avData[5] = [4, 1, 2, 3, 4]
For $n = 1 To $avData[0]
    ConsoleWrite("$n = " & $avData[$n] & @LF)
    If $n = 3 then $avData[0] -= 1
Next

:)


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

You lost me. Can you modify this code to show any difference between the two?

:P

I am interested in the answer as well... The "delete from the bottom up" method worked great... I asked my follow-up question because I tried, like you, to modify my original code with the suggested change and I could not make it work... But, experience has told me that just when I think I know the "best way" to get a task accomplished, someone comes along with a "better way"... So I figured I'd ask... :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I think it's just me but this used to work (i think);

#include <Array.au3>

Global $avData[10] = ["a1","a2","a3","a4","a5","b1","b2","b3","b4","b5"]
Dim $aNew[1]
$max = UBound($avData) - 1 ; $max = 10
For $n = 0 To $max
    If $n = 4 Then
        ;$max = $max - 3 ; suppose to change $max to 7 then end loop
        $max -= 3 ; suppose to change $max to 7 then end loop
    EndIf
    _ArrayAdd($aNew, $avData[$n])
Next
_ArrayDisplay($aNew, "Data1")
; $aNew suppose to only have elements up to "b2"

Now neither of them works. :/

Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

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