Opened 10 years ago
Last modified 9 months ago
#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)
comment:1 Changed 10 years ago by jguinch
comment:2 follow-up: ↓ 3 Changed 10 years ago by Melba23
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 in reply to: ↑ 2 Changed 10 years ago by anonymous
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 Changed 10 years ago by jguinch
I don't really understand what you mean in comment3...
comment:5 Changed 10 years ago by BrewManNH
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 Changed 10 years ago by anonymous
@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 Changed 4 years ago by Jpm
- Owner set to Jpm
- Status changed from new to assigned
Hi,
I sent a fix to Jon
comment:8 Changed 9 months ago by Jpm
- Owner changed from Jpm to Jon
Hi,
I look again to what I try to do,
My fix is not working
So perhaps Jon will have a solution
comment:9 Changed 9 months ago by Jpm
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
Guidelines for posting comments:
- You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
- In-depth discussions should take place on the forum.
For more information see the full version of the ticket guidelines here.
Also, this code is a good example of what happends :