Jump to content

Array In Func?


Recommended Posts

I'm trying to create a functions that has an unset amount of variables the user can input. What I want to do instead is create Array's in the function.

Example: Instead of Func MyFunc($Var1, $Inp1, $Var2, $Inp2, $Var3, $Inp3) Ect.

I would like Func MyFunc($Arr1[Var], $Arr2[Var], $Arr1[Var], $Arr2[Var]

It's a little more for the user to use but it'd mostly be me using it and it'd be a lot easier to create rather then typing out $Var1 - $Var99 or $Inp1 - $Inp99

Or if anyone has another way of creating an unset amount of variables I'm open for suggestions.

Edit: I created an example script

Func _MyFunc($iArr[$i], $sArr[$s], $iVar)
_OtherFunc($iArr[1], $sArr[1], $iVar)
_OtherFunc($iArr[2], $sArr[2], $iVar)
EndFunc

The other thing I didn't ask (I've done similar things to this before though) is how I could set it so it knows how many times to run _OtherFunc with what [$i]

- Acid Corps

Edited by AcidCorps
Link to comment
Share on other sites

I'm trying to create a functions that has an unset amount of variables the user can input. What I want to do instead is create Array's in the function.

Example: Instead of Func MyFunc($Var1, $Inp1, $Var2, $Inp2, $Var3, $Inp3) Ect.

I would like Func MyFunc($Arr1[Var], $Arr2[Var], $Arr1[Var], $Arr2[Var]

It's a little more for the user to use but it'd mostly be me using it and it'd be a lot easier to create rather then typing out $Var1 - $Var99 or $Inp1 - $Inp99

Or if anyone has another way of creating an unset amount of variables I'm open for suggestions.

Edit: I created an example script

Func _MyFunc($iArr[$i], $sArr[$s], $iVar)
_OtherFunc($iArr[1], $sArr[1], $iVar)
_OtherFunc($iArr[2], $sArr[2], $iVar)
EndFunc

The other thing I didn't ask (I've done similar things to this before though) is how I could set it so it knows how many times to run _OtherFunc with what [$i]

- Acid Corps

That is very confusing. Post a demo script that can actually be run to see what you are talking about.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

As I typed it I knew it was confusing but I cannot post a demo script as I don't have one

basically I want to replace the $var in the Func with $Array[$i] so I can have an unlimited amount

One way to pass an unlimited and variable number of parameters is to simply pass an array:

$iNum = Number(InputBox("Test", "How many parameter to pass?"))

If $iNum Then
    Dim $avParams[$iNum]
    For $n = 0 To UBound($avParams) - 1
        $avParams[$n] = $n * 3
    Next
    
    _MyFunc($avParams)
EndIf

Func _MyFunc($avInput)
    Local $sMsg = "Inputs:"
    For $x = 0 To UBound($avInput) - 1
        $sMsg &= @CRLF & $x & ": " & $avInput[$x]
    Next
    MsgBox(64, "_MyFunc()", $sMsg)
EndFunc   ;==>_MyFunc

Is your requirement more complicated than that?

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

As I typed it I knew it was confusing but I cannot post a demo script as I don't have one

basically I want to replace the $var in the Func with $Array[$i] so I can have an unlimited amount

Do you mean, using an array as parameter for a function? Or returning an array to be used later? Or something else?

Here is a small demo that does both. Does this help?

#include <array.au3>

Dim $a1[3] = [10,20,30]; create 3-element array with 10, 20 and 30 as elements

_ArrayDisplay($a1,"This is the array that will be sent to the function")

$aResult = _SmallArrayDemo($a1,2); show the first two elements of $a1 and store them in a new array $aResult

_ArrayDisplay($aResult,"This is the array returned by the function")

Exit

Func _SmallArrayDemo($tempArray,$amount)
; returns the first $amount elements in the sent $tempArray, multiplied by 100, as a new array
    Dim $resultToReturn[$amount]
    For $loop = 0 To $amount-1; arrays are 0 based
        MsgBox(0,"test","Array sent to function element "&$loop&" is: "&$tempArray[$loop])
        $resultToReturn[$loop] = $tempArray[$loop]*100
    Next
    Return $resultToReturn; return created result array
EndFunc

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Hi

What the heck , since everyones having a guess at your needs AcidCorps...lol

#include <GUIConstants.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

Global $Input[13], $x = 5, $y = 5 

$Gui = GUICreate("Pass Arrays to function as parameters", 170, 130)
For $d = 1 To 12
    If $d = 5 Then
        $x = 60
        $y = 5
    ElseIf $d = 9 Then
        $x = 115
        $y = 5
    EndIf   
    $Input[$d] = GUICtrlCreateInput("Input " & $d, $x, $y, 50, 20)
    $y += 25
Next
$ReadAll = GUICtrlCreateButton("Array Display Inputs", 5, 105, 160, 20)
GUICtrlSetOnEvent(-1,  "ReadInputs")
GUISetState(@SW_SHOW, $Gui)

While 1
    Sleep(100)
WEnd

Func ReadInputs()
    Dim $Param1[5], $Param2[5], $Param3[5]
    For $i = 1 To 12
        If $i < 5 Then $Param1[$i] = GUICtrlRead($Input[$i])
        If $i > 4 And $i < 9 Then $Param2[$i - 4] = GUICtrlRead($Input[$i])
        If $i > 8 Then $Param3[$i - 8] = GUICtrlRead($Input[$i])
    Next
    DisplayArrays($Param1, $Param2, $Param3)    
EndFunc

Func DisplayArrays($iPar1, $iPar2, $iPar3)
    _ArrayDisplay($iPar1, "Input read 1 To 4")
    _ArrayDisplay($iPar2, "Input read 5 To 8")
    _ArrayDisplay($iPar3, "Input read 9 To 12")
EndFunc 

Func Close()
    Exit
EndFunc

Cheers

Link to comment
Share on other sites

So you are trying to make an unlimited number of parameters? I think you can have up to 255... Just make them optional.

Func _Myfunc($Param1=0,$Param2=0,$Param3=0);continue to as many as you may need up to 255

Only way i know of to do it... *Shrug*

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