Opened on Nov 6, 2012 at 7:18:21 PM
Last modified on Feb 20, 2024 at 3:46:06 PM
#2279 assigned Bug
For loop not working as expected
| Reported by: | BrewManNH | Owned by: | Jon |
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | 3.3.8.1 | Severity: | None |
| Keywords: | Cc: |
Description
Here's some code I came across that isn't working as it should, at least isn't working as I think it should work.
Global $Array[5] = ["AA", "BB", "CC", "DD", "EE"]
For $I = -1 To $Array
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $I = ' & $I & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
Next
Global $Array[5][2] = [["AA", "1"], ["BB", 2], ["CC", 3], ["DD", 4],["EE", 5]]
For $I = 10 To $Array
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $I = ' & $I & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
Next
The output from the code above, when run from SciTE, is this.
@@ Debug(7) : $I = -1
Error code: 0
@@ Debug(7) : $I = BB
Error code: 0
@@ Debug(7) : $I = CC
Error code: 0
@@ Debug(7) : $I = DD
Error code: 0
@@ Debug(7) : $I = EE
Error code: 0
@@ Debug(11) : $I = 10
Error code: 0
@@ Debug(11) : $I = 10
Error code: 0
@@ Debug(11) : $I = 10
Error code: 0
@@ Debug(11) : $I = 10
Error code: 0
@@ Debug(11) : $I = 10
The first value of $I is correct, it starts at whatever the For loop starts at, but then the For/Next loop starts acting like a For/In/Next loop. On a 2D array, the second section of code, it just loops for however many elements there are in the array, and the initial value of $I doesn't change.
I would have expected this to either error out, because it's referencing an array instead of a number, or run once because there's no end number to reach.
I have tested this code on 3.3.8.1, 3.3.6.1, and 3.3.9.5 and they all run the same way.
I don't think this should be working the way it is and appears to be a bug.
Attachments (0)
Change History (6)
comment:2 by , on Jul 21, 2013 at 11:31:57 PM
| Resolution: | → Rejected |
|---|---|
| Status: | new → closed |
comment:3 by , on Sep 6, 2013 at 12:52:49 PM
I think the script must error as $array as no valid type conversion
comment:4 by , on Jun 7, 2022 at 9:39:22 PM
| Resolution: | Rejected |
|---|---|
| Status: | closed → reopened |
comment:5 by , on Jun 7, 2022 at 9:41:55 PM
| Owner: | set to |
|---|---|
| Status: | reopened → assigned |
For invalid parameter (cannot be incremented) will produce a Fatal error
Even similar expression such $var = $array + 1 will produce a Fatal error.
Fix sent to Jon
comment:6 by , on Feb 20, 2024 at 3:46:06 PM
| Owner: | changed from to |
|---|

Yes, it's a bug. The code isn't performing necessary type conversion. The fix is trivial.
Thanks for reporting.