Opened 6 years ago

Last modified 8 months ago

#3003 assigned Bug

Using a function call in an array assignment causes 2 function calls

Reported by: jguinch Owned by: Jpm
Milestone: Component: AutoIt
Version: Severity: None
Keywords: Cc:


With this code :

Local $aArray[1]

$aArray[ _myFunction() ] = "myString"

Func _myFunction()
	ConsoleWrite("_myFunction" & @CRLF)
	Return 0

"myFunction" appears two times in the output console, but it should once.

Attachments (0)

Change History (7)

comment:1 Changed 6 years ago by jguinch

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

Local $aArray[1]
$aArray[  0 * MsgBox(0, "", "Appears twice")  ] = MsgBox(0, "", "Appears once")

comment:2 follow-up: Changed 6 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"


$hFunction = _Function()
$aArray[ $hFunction ] = "myString"

Func _Function()
	ConsoleWrite("_Function @ " & @MSEC & @CRLF)
	Return 0


comment:3 in reply to: ↑ 2 Changed 6 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 6 years ago by jguinch

I don't really understand what you mean in comment3...

comment:5 Changed 6 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"
Func _myFunction()
    Local Static $Return = -1
	ConsoleWrite("_myFunction = " & $Return & @CRLF)
    $Return += 1
	Return $Return

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 6 years ago by anonymous

@BrewManNH : you pointed exatly what I was trying to do to assist a forum user in this thread :

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"


Func _i() 
	Local Static $iIndex = -1
	$iIndex += 1
	Return $iIndex

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 8 months ago by Jpm

  • Owner set to Jpm
  • Status changed from new to assigned

I sent a 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.

Add Comment

Modify Ticket

as assigned The owner will remain Jpm.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.