Jump to content

really simple array question...


Recommended Posts

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( ^ ERROR

so 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 = ^ ERROR

so 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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...