Jump to content

is there a way to pass an array to a function directly like function(["one","two","three"])?


Recommended Posts

is there a way to pass an array to a function directly like function(["one","two","three"])?

In some cases I don't have to use the array outside the the function so I would like to avoid to do an array declaration before calling the function:

local $array = ["one","two","three"]

function($array)

Link to comment
Share on other sites

No, I guess Autoit doesnt support that,

why not pass the array and then check the values using a For Loop :huh:

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Like this?

#include <Array.au3> ; For ArrayDisplay

Dim $aArray[3]=["1", "2", "3"]

$aString =_ArrayToString($aArray, "|")
$aSplit = StringSplit($aString, "|")
_MyFunc($aArray, "Test")

Func _MyFunc($array, $Par2 = "")
_ArrayDisplay($array,"")
MsgBox(0, 0, $array[0])
MsgBox(0, 0, $Par2)
EndFunc
Edited by johnmcloud
Link to comment
Share on other sites

Two things:

Dim should regarded as obsolescent. Prefer using Local or Global depending on requirements.

Arrays can be costly to copy (AutoIt uses copy on write but anyway). Pass arrays ByRef (see help).

Recommended option: use the Const modifier as appropriate.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

is there a way to pass an array to a function directly like function(["one","two","three"])?

In some cases I don't have to use the array outside the the function so I would like to avoid to do an array declaration before calling the function:

local $array = ["one","two","three"]

function($array)

Maybe ByRef ? From the helpfile:

Example1()
Example2()

; example1
Func Example1()
; Sample script with three user-defined functions
; Notice the use of variables, ByRef, and Return

Local $foo = 2
Local $bar = 5
MsgBox(0, "Today is " & today(), "$foo equals " & $foo)
swap($foo, $bar)
MsgBox(0, "After swapping $foo and $bar", "$foo now contains " & $foo)
MsgBox(0, "Finally", "The larger of 3 and 4 is " & max(3, 4))
EndFunc ;==>Example1

Func swap(ByRef $a, ByRef $b) ;swap the contents of two variables
Local $t
$t = $a
$a = $b
$b = $t
EndFunc ;==>swap

Func today() ;Return the current date in mm/dd/yyyy form
Return (@MON & "/" & @MDAY & "/" & @YEAR)
EndFunc ;==>today

Func max($x, $y) ;Return the larger of two numbers
If $x > $y Then
Return $x
Else
Return $y
EndIf
EndFunc ;==>max

;End of sample script 1

; example2
Func Example2()
; Sample scriptusing @NumParams macro
Test_Numparams(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
EndFunc ;==>Example2

Func Test_Numparams($v1 = 0, $v2 = 0, $v3 = 0, $v4 = 0, $v5 = 0, $v6 = 0, $v7 = 0, $v8 = 0, $v9 = 0, _
$v10 = 0, $v11 = 0, $v12 = 0, $v13 = 0, $v14 = 0, $v15 = 0, $v16 = 0, $v17 = 0, $v18 = 0, $v19 = 0)
#forceref $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9, $v10, $v11, $v12, $v13, $v14, $v15, $v16, $v17, $v18, $v19
Local $val
For $i = 1 To @NumParams
$val &= Eval("v" & $i) & " "
Next
MsgBox(0, "@NumParams example", "@NumParams =" & @NumParams & @CRLF & @CRLF & $val)
EndFunc ;==>Test_Numparams

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

imbuter2000,

I would like to avoid to do an array declaration before calling the function:

Why? If you explain your purpose there may be other alternatives.

kylomas

edit: additional info

This

#include <array.au3>
_1('123')
func _1($prm)
 local $array = stringsplit($prm,'')
 _arraydisplay($array)
endfunc

and/or variations of what J1 posted are as close as you are likely to get.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

If the arguments will never change then here is a way (no pun intended):

Func my_awesome_function()
    Local Const $my_awesome_array[3] = [1, 2, 3]
    
    ; do things
EndFunc

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