autorayman Posted December 28, 2011 Share Posted December 28, 2011 Hi forks,I have a small question about passing array into functions. As you can see, most values can be passed directly without wraping into a variable:myfunction( 1, 'a' )But when I do this with an array, it result in error:==> Error in expression.: myfunction( [1,'a'] ) myfunction( ^ ERRORso what is the expected correct expression ? btw, it seem that the size of array must be specified:==> Subscript used with non-Array variable.: dim $xy = [ 1, "a" ] dim $xy = ^ ERRORso arrays can only be assigned like this.dim $xy[2] = [ 1, 'a' ]Is there any less definitive way of assigning arraies ?I am running AutoItv3. Please help.Thanks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 28, 2011 Moderators Share Posted December 28, 2011 autorayman, Welcome to the AutoIt forum. You need to pass the array name to the function: ; Do nto use Dim - use either Global or Local Global $xy[2] = [1, 'a'] _myfunction($xy) ; You can also assign the array elements like this $xy[0] = 10 $xy[1] = "Fred" _myfunction($xy) Func _myfunction($aArray) MsgBox(0, "Variables", "Element [0] = " & $aArray[0] & @CRLF & "Element[1] = " & $aArray[1]) EndFunc All clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
martin Posted December 28, 2011 Share Posted December 28, 2011 (edited) If you want to pass an array to a function then you can do something like this Dim $aG[9] = [2,4,0,6,7,8,4,4,1] $aG[3] = 17 Redim $aG[11] ;add elements to the array, set values remain Dim $bb $bb = $aG; now $bb is a copy of the array $aG $aG[10] = 23 $Result = MyFunc($aG) Function $aG($aA) if not isArray($aA) then return '' doSOmethingWIthArray endfunc You need to spend some time reading the help. EDIT: 7 minutes after M23's post! I must be writing slower than ever. Edited December 28, 2011 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
autorayman Posted December 28, 2011 Author Share Posted December 28, 2011 Hi, Malba, martin, Thank you for the quick help. Yes, I am not quite used to autoit but more familiar with python. This might explain why I have so many question about the basic things. So now I see, the array must be named before use. And I must speicify the size of the array when assigning it. What I initailly want to do is to find a handy way to pass a function arbitrary number of parameters. I see in the Help doc, it's done by defining enough optional parameters. Is there any easier way to achive this without defining many options ahead of time (in my case could be up to 12 parameters)? Please shed some light . Thanks. Link to comment Share on other sites More sharing options...
JohnOne Posted December 28, 2011 Share Posted December 28, 2011 Passing an array would be best in your case then. It all depends how you are creating your array. Maybe it's from user input, and ReDim might be good for you. But other functions like for example StringSplit will create an array for you. You must create your function to deal with a variable array size, look at UBound() function for that. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
autorayman Posted December 28, 2011 Author Share Posted December 28, 2011 Hi John. OK, Then array will be the way to go for me. Actually, orignaly I was thinking about something like myfunction( [ $a, $b, $c ] ). which looks pretty friendly to people who are reluctant to declare everything before use. Lazy me. I will try to get use to the autoit way. : ) Thanks Link to comment Share on other sites More sharing options...
JohnOne Posted December 28, 2011 Share Posted December 28, 2011 Well you can use a lot of optional paramaters, but that's only really if you have a max amount of them. Here is a little example. _MyFunc() _MyFunc(1) _MyFunc(1,2) _MyFunc(1,2,3,4) Func _MyFunc($Param1 = 0, $Param2 = 0, $Param3 = 0, $Param4 = 0 ,$Param5 = 0) MsgBox(0,"How many params were passed", @NumParams) EndFunc AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Malkey Posted December 28, 2011 Share Posted December 28, 2011 Here is another example. ; Do not use Dim - use either Global or Local Local $xy = "1, a,b" ; Enter any number of values separated by a coma, or some other string. Local $aArray = _myfunction($xy, ", ") ; Note "coma and space" used as separator. Local $sDisplay = "" For $i = 0 To UBound($aArray) - 1 $sDisplay &= "Element [" & $i & "] = " & $aArray[$i] & @LF Next MsgBox(0, "Variables", StringTrimRight($sDisplay, 1)) Func _myfunction($sString, $sSeparator = ",") ; "One coma only" as default separator. Local $aRetArray ;$aRetArray = StringSplit($sString, $sSeparator, 3) ;or $aRetArray = StringRegExp($sString & $sSeparator, "h*(.*?)h*" & $sSeparator, 3) ; Removes leading and trailing spaces if present. ;.... More Return $aRetArray EndFunc ;==>_myfunction #cs MsgBox output:- Element [0] = 1 Element [1] = a,b #ce Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now