Recently Browsing 0 members
No registered users viewing this page.
I'm trying to pass a nested array to a function, such that the function alters the inner array.
I was surprised to find that this minimal reproducible example, despite its use of ByRef, seems to pass a copy of the inner array to the function:
#include <Array.au3> ; a boring old array Local $aInnerArray = [1, 2, 3, 4, 5] ; a one-element array containing a reference to the other array Local $aOuterArray = [$aInnerArray] ; intention: take a nested array and alter its inner array ; reality: the inner array seems to be getting copied Func ChangeIt(ByRef $aOuter) Local $aInner = $aOuter $aInner = 0 EndFunc ; Expected: [1, 2, 3, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✔ _ArrayDisplay($aInnerArray, 'Before') ; $aOuterArray passed by-ref, should receive reference to $aInnerArray ; Therefore should change $aInnerArray to [1, 2, 0, 4, 5] ChangeIt($aOuterArray) ; Expected: [1, 2, 0, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✘ _ArrayDisplay($aInnerArray, 'After') I suspect that either:
the copy is taking place in the first line of the function (I couldn't find a way to access the inner array without first assigning it to a variable though); or ByRef doesn't propagate into inner levels of the data structure being passed, which seems less likely to me. Could someone please point me in the right direction to get this working as intended?
Update: the answer
; WRONG: ; a one-element array containing a reference to the other array Local $aOuterArray = [$aInnerArray] The assumption I made about this code is wrong—it actually copies $aInnerArray into $aOuterArray, so there are now two unrelated $aInnerArray instances. It is not possible to store arrays in other arrays by reference.
If it is necessary to refer to a mutable array in multiple places, consider holding it in a global variable. Where a collection of mutable arrays needs to be accessed in multiple places (as in my case), consider storing them in a global array and referring to each sub-array by index (also known as the Registry pattern).
I've an error and don't know why and hope somebody can help me..
I think maybe i din't include somethin for the "ByRef" function, but i could be wrong :S
Anyway here is what i included ...
#include <ButtonConstants.au3> #include <ComboConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <File.au3> ; for _FileCreate ed. #Include <GuiComboBoxEx.au3> ; for _GUICtrlComboBoxEx_ResetContent #include "EzMySql.au3" #include <Array.au3> This is what i do before the error starts ... (everything else is running properly)
$query = "SELECT * FROM `users` ;" $hostname = Read_from_ini("app","server","ip","no_server_ip") $dbname = "blokkerreferencedata" $usrname = Read_from_ini("app","server","username","no_server_usr") $Pass = Read_from_ini("app","server","pass","no_server_pass") $SQLport = Read_from_ini("app","server","sql_port","no_server_port") $iresult_rows = "" $iresult_colums = "" $iresult = get_result($query,$hostname,$dbname,$usrname,$Pass,$SQLport,ByRef $iresult_rows,ByRef $iresult_colums) _ArrayDisplay($iresult,"result") Here is the error i get...
==> Error in expression.: $iresult = get_result($query,$hostname,$dbname,$usrname,$Pass,$SQLport,ByRef $iresult_rows,ByRef $iresult_colums) $iresult = get_result($query,$hostname,$dbname,$usrname,$Pass,$SQLport,^ ERROR does somebody see what i do wrong here???
thanks in advanced.
ps. if it isn't a forgotten "include" --> how does somebody know whish include to use???
I have function which needs 3 parameters and the last one is passed ByReference.
But can I make the reference parameter as optional.i.e., $countArgts as optional or to keep a default value as below.
This is giving a compilation error, or any method overloading approach like in Programming languages Java,etc.
I was working on a good way to run a script after x amount of time without needing to run it as a scheduled task so that it can be run from any computer. I have used AdlibRegister() before with good success for small periods of time but never larges ones (like an hour). So I tried it out and it looks like it actually works very well.
One thing that sort of annoyed me though was walking up to the computer and not knowing when it was going to run next, so I tried to plug in a little time check function and it appeared to work on my little test but in production it started to give me some crazy results.
Breaking it down, I figured most of the issue had to do with two things.
1.) Bad practice to use Global Variables in a function. I never understood why, so I try not to do it. In this example I would need MyFunc() to update $vEndTime and $vStartTime globally so I believe the right way to do this would be to pass the variables to the function ByRef. But that leads to problem #2
2.) You can not pass any variables to a function called with AdlibRegister()
So that leads me to create this topic so that I can learn some new best practices.
Why exactly is using Global in the function a bad idea on a simple script like this? How can I pass these to/from the function using AdlibRegister() and if it can't really be done, what is the best way to do this?
For now I have my script working without the "check time" feature and I could update/return multiple values from the function with Global scope but I create this thread in hopes to learn more of why/how things can and should not be done. Also maybe a good tip on how to run a function on a repeat basis with accurate time without AdlibRegister()
Edit: Just realized I did not post the example code.
;Just a concept this is not my actual script where things were a bit different #Include <Date.au3> Global $iWaitTime = 60000*60 Global $vStartTime = _NowTime(4) HotKeySet("^!t", "CheckTime") AdlibRegister("MyFunc", $iWaitTime) MyFunc() While 1 Sleep(10) WEnd Func MyFunc() $vEndTime = $vStartTime $vStartTime = _NowTime(4) ;do stuff ;do stuff EndFunc Func CheckTime() MsgBox(0, "", "Current Cycle is set for " & $iWaitTime/60000 & " Minutes" & @CRLF & @CRLF & _ "There is " & ($iWaitTime/60000)-(_NowTime(4)-$vStartTime) & " Minutes Left Until Next Cycle") EndFunc The actual code was a bit different, I had to strip the colon out of the time and such.
Reading the help file I noticed the directive "#forceref" in sereral sample codes, e.g. for "ByRef" in this line:
#forceref $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9, $v10, $v11, $v12, $v13, $v14, $v15, $v16, $v17, $v18, $v19
What is it's effect? searching the help file, I can find it "outside" of sample code just for this entry:
Au3check syntax tool
#forceref $varname [, ...] can be used for inside functions, like the following: Func Test_Numparams($v1 = 0, $v2 = 0, $v3 = 0, $v4 = 0, $v5 = 0, $v6 = 0, $v7 = 0, $v8 = 0, $v9 = 0) #forceref $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9
Same question for #forcedef
From this postings here I don't get the exact effect either: