Jump to content

UDF return an array


Recommended Posts

I am new in AutoIt.

Anyone help me to understand that How can I write the UDF to return an array. Something like following:

Func _ChoseDate()
    Local $n1,$n2, $msg, $button_1
        Local $arrTest[2]

    GUICreate("Get the movements", 140, 140, 800, 200)
    $n1 = GUICtrlCreateDate("", 20, 20, 100, 20, $DTS_SHORTDATEFORMAT)
    $n2=GUICtrlCreateDate("",20,60,100,20,$DTS_SHORTDATEFORMAT)

    ; to select a specific default format
    ;$DTM_SETFORMAT_ = 0x1032   ; $DTM_SETFORMATW
    ;$style = "yyyy/MM/dd HH:mm:ss"
    ;GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
    ;GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)

    ; Create the controls
    $button_1 = GUICtrlCreateButton("&OK", 20, 100, 100, 30)
    GUISetState()

    ; In this message loop we use variables to keep track of changes to the radios, another
    ; way would be to use GUICtrlRead() at the end to read in the state of each control
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ;This function will return 0 if the user does not want to run this
                Return 0
                GUIDelete()
                Exit
            Case $msg = $button_1
                ;Return the chosen date
                $DTM_SETFORMAT_ = 0x1032    ; $DTM_SETFORMATW
                $style = "yyyy/MM/dd HH:mm:ss"
                GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
                GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)
                $iDateCalc = _DateDiff( 'D',GUICtrlRead($n1),GUICtrlRead($n2))
                if $iDateCalc<0 Then
                    return 0
                    GUIDelete()
                    Exit
                Else
                    $arrTest[0]=GUICtrlRead($n1)
                                    $arrTest[1]=GUICtrlRead($n2)
                    Return $arrTest
                GUIDelete()
                Exit
                EndIf
        EndSelect
    WEnd
EndFunc

Tks,

LVD

Link to comment
Share on other sites

  • Moderators

How about you explain in a lot more detail.

Your example shows an array being returned.

Edit:

Unless you mean literally how to, and you're not understanding how that UDF does it?

#include <Array.au3>; Used only to use _ArrayDisplay
Global $a_var = _MyFunc()
_ArrayDisplay($a_var)

Func _MyFunc()
    Local $a_array[2] = ["element 0", "element 1"]
    Return $a_array; where $a_array is your array variable
EndFunc

Edit:

Edited the edit :mellow:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I am new in AutoIt.

Anyone help me to understand that How can I write the UDF to return an array. Something like following:

How do you know it's not returning an array? A common mistake is to check by just trying to display the array in MsgBox() or ConsoleWrite(), which doesn't work.

You can get a boolean test with IsArray(), or look at it with _ArrayDisplay() (see help file).

:mellow:

Edit: This works fine, and the only change to your function is correcting your spelling of "Choose":

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <DateTimeConstants.au3>
#include <Array.au3>

$aRET = _ChooseDate()
If IsArray($aRET) Then
    _ArrayDisplay($aRET, "$aRET")
Else
    MsgBox(16, "Error", "Did not return array:" & @CRLF & _
            "$aRET = " & $aRET & "; Type = " & VarGetType($aRET))
EndIf

Func _ChooseDate()
    Local $n1, $n2, $msg, $button_1
    Local $arrTest[2]

    GUICreate("Get the movements", 140, 140, 800, 200)
    $n1 = GUICtrlCreateDate("", 20, 20, 100, 20, $DTS_SHORTDATEFORMAT)
    $n2 = GUICtrlCreateDate("", 20, 60, 100, 20, $DTS_SHORTDATEFORMAT)

    ; to select a specific default format
    ;$DTM_SETFORMAT_ = 0x1032   ; $DTM_SETFORMATW
    ;$style = "yyyy/MM/dd HH:mm:ss"
    ;GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
    ;GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)

    ; Create the controls
    $button_1 = GUICtrlCreateButton("&OK", 20, 100, 100, 30)
    GUISetState()

    ; In this message loop we use variables to keep track of changes to the radios, another
    ; way would be to use GUICtrlRead() at the end to read in the state of each control
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ;This function will return 0 if the user does not want to run this
                Return 0
                GUIDelete()
                Exit
            Case $msg = $button_1
                ;Return the chosen date
                $DTM_SETFORMAT_ = 0x1032 ; $DTM_SETFORMATW
                $style = "yyyy/MM/dd HH:mm:ss"
                GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
                GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)
                $iDateCalc = _DateDiff('D', GUICtrlRead($n1), GUICtrlRead($n2))
                If $iDateCalc < 0 Then
                    Return 0
                    GUIDelete()
                    Exit
                Else
                    $arrTest[0] = GUICtrlRead($n1)
                    $arrTest[1] = GUICtrlRead($n2)
                    Return $arrTest
                    GUIDelete()
                    Exit
                EndIf
        EndSelect
    WEnd
EndFunc   ;==>_ChooseDate

:P

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

How do you know it's not returning an array? A common mistake is to check by just trying to display the array in MsgBox() or ConsoleWrite(), which doesn't work.

You can get a boolean test with IsArray(), or look at it with _ArrayDisplay() (see help file).

:mellow:

Edit: This works fine, and the only change to your function is correcting your spelling of "Choose":

#include <GuiConstantsEx.au3>
#include <Date.au3>
#include <DateTimeConstants.au3>
#include <Array.au3>

$aRET = _ChooseDate()
If IsArray($aRET) Then
    _ArrayDisplay($aRET, "$aRET")
Else
    MsgBox(16, "Error", "Did not return array:" & @CRLF & _
            "$aRET = " & $aRET & "; Type = " & VarGetType($aRET))
EndIf

Func _ChooseDate()
    Local $n1, $n2, $msg, $button_1
    Local $arrTest[2]

    GUICreate("Get the movements", 140, 140, 800, 200)
    $n1 = GUICtrlCreateDate("", 20, 20, 100, 20, $DTS_SHORTDATEFORMAT)
    $n2 = GUICtrlCreateDate("", 20, 60, 100, 20, $DTS_SHORTDATEFORMAT)

    ; to select a specific default format
    ;$DTM_SETFORMAT_ = 0x1032   ; $DTM_SETFORMATW
    ;$style = "yyyy/MM/dd HH:mm:ss"
    ;GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
    ;GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)

    ; Create the controls
    $button_1 = GUICtrlCreateButton("&OK", 20, 100, 100, 30)
    GUISetState()

    ; In this message loop we use variables to keep track of changes to the radios, another
    ; way would be to use GUICtrlRead() at the end to read in the state of each control
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ;This function will return 0 if the user does not want to run this
                Return 0
                GUIDelete()
                Exit
            Case $msg = $button_1
                ;Return the chosen date
                $DTM_SETFORMAT_ = 0x1032 ; $DTM_SETFORMATW
                $style = "yyyy/MM/dd HH:mm:ss"
                GUICtrlSendMsg($n1, $DTM_SETFORMAT_, 0, $style)
                GUICtrlSendMsg($n2, $DTM_SETFORMAT_, 0, $style)
                $iDateCalc = _DateDiff('D', GUICtrlRead($n1), GUICtrlRead($n2))
                If $iDateCalc < 0 Then
                    Return 0
                    GUIDelete()
                    Exit
                Else
                    $arrTest[0] = GUICtrlRead($n1)
                    $arrTest[1] = GUICtrlRead($n2)
                    Return $arrTest
                    GUIDelete()
                    Exit
                EndIf
        EndSelect
    WEnd
EndFunc   ;==>_ChooseDate

:P

Thank you. Solved.

Anyway, what's wrong with my UDF above.

LVD

Edited by levanduyet
Link to comment
Share on other sites

Anyway, what's wrong with my UDF above.

???

:P

OK, I'll be Laurel: "I don't know, Stanley, what's wrong with your UDF above?"

:mellow:

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

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