Opened on Mar 23, 2015 at 10:58:00 PM
Last modified on Feb 26, 2024 at 5:51:05 PM
#3003 assigned Bug
Using a function call in an array assignment causes 2 function calls
| Reported by: | jguinch | Owned by: | Jon |
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | 3.3.12.0 | Severity: | None |
| Keywords: | Cc: |
Description
With this code :
Local $aArray[1]
$aArray[ _myFunction() ] = "myString"
Func _myFunction()
ConsoleWrite("_myFunction" & @CRLF)
Return 0
EndFunc
"myFunction" appears two times in the output console, but it should once.
Attachments (0)
Change History (9)
follow-up: 3 comment:2 by , on Mar 26, 2015 at 4:44:12 PM
Interesting that it does not do the same when you set the function to a variable:
Local $aArray[1]
$aArray[ _Function() ] = "myString"
ConsoleWrite(@CRLF)
$hFunction = _Function()
$aArray[ $hFunction ] = "myString"
Func _Function()
ConsoleWrite("_Function @ " & @MSEC & @CRLF)
Sleep(100)
Return 0
EndFunc
M23
comment:3 by , on Mar 26, 2015 at 5:15:17 PM
Replying to Melba23:
Interesting that it does not do the same when you set the function to a variable
Not very interesting. You assign the return value of the function to a variable and then evaluate that variable expression (twice).
comment:4 by , on Mar 26, 2015 at 10:19:26 PM
I don't really understand what you mean in comment3...
comment:5 by , on Mar 28, 2015 at 8:22:21 PM
You're calling the function to get the returned value for the array assignment, then you're calling it again when you actually set the value to the array. Run this modification to your code to see what's happening.
#include <Array.au3>
Local $aArray[2]
$aArray[ _myFunction() ] = "myString"
_ArrayDisplay($aArray)
Func _myFunction()
Local Static $Return = -1
ConsoleWrite("_myFunction = " & $Return & @CRLF)
$Return += 1
Return $Return
EndFunc
It calls the function the first time to see what element of the array to set it to, then when AutoIt does the actual declaration for the element it calls it again to see where it's going. You'll notice that the only element with anything in it is $aArray[1] because that's what the return value is when you set the string to the array.
So, I don't know if this is a bug, but it looks like a bad idea to use a function call to determine where in an array to put something, at least don't do it in the actual declaration statement, because you won't be able to be 100% sure it's going where you think it's going.
comment:6 by , on Mar 28, 2015 at 10:16:23 PM
@BrewManNH : you pointed exatly what I was trying to do to assist a forum user in this thread : http://www.autoitscript.com/forum/topic/169135-flexibility-with-declaring-an-array/
I wanted to post this kind of code :
#Include <Array.au3> Local $a[10] $a[ _i() ] = "First row" $a[ _i() ] = "Second row" $a[ _i() ] = "Third row" _ArrayDisplay($a) Func _i() Local Static $iIndex = -1 $iIndex += 1 Return $iIndex EndFunc
But when I saw the result, I decided to write a ticket here.
For me, it is a bug, but you decid :-)
If not, maybe something about this "bad" uasge should appear in the help file
comment:7 by , on Sep 3, 2020 at 1:22:21 PM
| Owner: | set to |
|---|---|
| Status: | new → assigned |
Hi,
I sent a fix to Jon
comment:8 by , on Feb 23, 2024 at 4:29:44 PM
| Owner: | changed from to |
|---|
Hi,
I look again to what I try to do,
My fix is not working
So perhaps Jon will have a solution
comment:9 by , on Feb 26, 2024 at 5:51:05 PM
Hi, I think I found a working solution
It was hard speially when both sie refer to subcript defined by array elements
Sent Fix to Jon

Also, this code is a good example of what happends :