Sign in to follow this  
Followers 0
jmichae3

can't return or pass literal array 3.3.13.19

4 posts in this topic

#1 ·  Posted (edited)

$a=[-1,-1,$pre,$mid,$post];;not found, do nothing
return $a 

what I would really like to do is

return [-1,-1,$pre,$mid,$post];;not found, do nothing

but I get from au3check:

D:prjkitchentimerkitchentimer-4.2time-funcs.au3(919,16) : ERROR: syntax error
            $a=[
~~~~~~~~~~~~~~~^

Mon 12/22/2014 20:56:10.15|D:prjkitchentimerkitchentimer-4.2||>au3check --version
AutoIt3 Syntax Checker v1.54.22  Copyright © 2007-2011 Tylo & AutoIt Team

autoit-3.3.13.19

it seems like a reasonable thing to be able to do with a high-level language.

I also would like to pass literal arrays as arguments to functions as if they were a variable. this is possible in c++ using initializer_list. I know this is not c++. but it would make my life and a lot of other people's lives a lot simpler.

Edited by jmichae3

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Initialize with Global, Local, Const, Dim

eg.

Func _somefunc($pre, $mid, $post)
   Local $a[] = [-1,-1,$pre,$mid,$post]
   Return $a
EndFunc

Edit:

FYI, there are several ways to create and work with funcs.

Just a few standard funcs off the top of my head:

StringSplit()

FileReadToArray()

StringRegExp()

StringToASCIIArray()

You could make your own initializing func with StringRegExp() and or StringSplit() if you wanted.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

jmichae3,

Like this...

#include <array.au3>

_arraydisplay( _functest() )

func _functest()
    return stringsplit('-1,-1,val1,val2',',')
endfunc

Use the same kind of thing for passing a string as an array.

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@kylomas

That of course is a simple way, and a way that I described above that you provided an example for.

But there are a lot of variances you'd have to take into account.

I'm not even sure this would scratch the surface, but I was thinking something like:

#include <Array.au3>

; ** Note **
;  The only way that vars will initiate, is if they are global scope unless you initiated first
Global $gsVar = "Gift"

Global $gszInit1D = '[-1, 2, 3, Default, "I''m", "Getting", ''A'', $gsVar]'

Global $ga1D = _ArrayInitialize($gszInit1D)
_ArrayDisplay($ga1D)

For $i = 0 To UBound($ga1D) - 1
    ConsoleWrite("[" & $i & "] = " & $ga1D[$i] & " : Type = " & VarGetType($ga1D[$i]) & @CRLF)
Next

Global $gszInit2D = '[[-1, 0x1B, 3, Default, "Don''t", "Look", ''A'', $gsVar, ''Horse''],' & _
    '[18, 44, 33, 9, "In", "The", ''Mouth'', True, False]]'

Global $ga2D = _ArrayInitialize($gszInit2D)
_ArrayDisplay($ga2D)

For $i = 0 To UBound($ga2D) - 1
    For $j = 0 To UBound($ga2D, 2) - 1
        ConsoleWrite("[" & $i & "][" & $j & "] = " & $ga2D[$i][$j] & " : Type = " & VarGetType($ga2D[$i][$j]) & @CRLF)
    Next
Next

Func _ArrayInitialize($sInitialize)

    Local $aDims = StringRegExp($sInitialize, "[\]]+\z", 3)
    If @error Then ; no initilizer
        Return SetError(1, 0, 0)
    EndIf

    Local $iDims = StringLen($aDims[0])
    ; let's keep it simple, shall we
    If $iDims < 1 Or $iDims > 2 Then
        Return SetError(2, 0, 0)
    EndIf

    ; double quoted strings
    Local $aDQ = StringRegExp($sInitialize, '"(.+?)"(?!")', 3)
    For $i = 0 To UBound($aDQ) - 1
        $sInitialize = StringRegExpReplace($sInitialize, '(?<!")\Q"' & $aDQ[$i] & '"\E(?!")', "DQREP" & StringFormat("%03d", $i))
    Next

    ; single quoted strings
    Local $aSQ = StringRegExp($sInitialize, "'(.+?)'(?!')", 3)
    For $i = 0 To UBound($aSQ) - 1
        $sInitialize = StringRegExpReplace($sInitialize, "(?<!')\Q'" & $aSQ[$i] & "'\E(?!')", "SQREP" & StringFormat("%03d", $i))
    Next

    $sInitialize = StringStripWS($sInitialize, 8)

    Local $aInd, $aRet
    Switch $iDims
        Case 1
            $aInd = StringSplit(StringRegExpReplace($sInitialize, "\[|\]", ""), ",", 2)
            Dim $aRet[UBound($aInd)]
            For $i = 0 To UBound($aInd) - 1
                Select
                    Case StringLeft($aInd[$i], 5) = "DQREP"
                        $aRet[$i] = $aDQ[Int(StringRight($aInd[$i], 3))]
                    Case StringLeft($aInd[$i], 5) = "SQREP"
                        $aRet[$i] = $aSQ[Int(StringRight($aInd[$i], 3))]
                    Case StringIsInt($aInd[$i]) Or StringRegExp($aInd[$i], "(?i)0x\d+")
                        $aRet[$i] = Int($aInd[$i])
                    Case StringIsFloat($aInd[$i])
                        $aRet[$i] = Number($aInd[$i])
                    Case Else; this is for vars and keywords
                        $aRet[$i] = Execute($aInd[$i])
                EndSelect
            Next
        Case 2
            $aDims = StringRegExp($sInitialize, "(?:\[|,)\[(.+?)\]", 3)
            If @error Then
                Return SetError(3, 0, 0)
            EndIf
            $aInd = StringSplit($aDims[0], ",", 2)
            Dim $aRet[UBound($aDims)][UBound($aInd)]
            For $idim = 0 To UBound($aDims) - 1
                $aInd = StringSplit($aDims[$idim], ",", 2)
                For $i = 0 To UBound($aInd) - 1
                    Select
                        Case StringLeft($aInd[$i], 5) = "DQREP"
                            $aRet[$idim][$i] = $aDQ[Int(StringRight($aInd[$i], 3))]
                        Case StringLeft($aInd[$i], 5) = "SQREP"
                            $aRet[$idim][$i] = $aSQ[Int(StringRight($aInd[$i], 3))]
                        Case StringIsInt($aInd[$i]) Or StringRegExp($aInd[$i], "(?i)0x\d+")
                            $aRet[$idim][$i] = Int($aInd[$i])
                        Case StringIsFloat($aInd[$i])
                            $aRet[$idim][$i] = Number($aInd[$i])
                        Case Else; this is for vars and keywords
                            $aRet[$idim][$i] = Execute($aInd[$i])
                    EndSelect
                Next
            Next
    EndSwitch

    Return $aRet
EndFunc 

Where they try to maintain their original data types etc.

And yes, the world would be so much more fun if you could just do something like:

Func __ArrayInitialize($sInitialize)

    Local $aDims = StringRegExp($sInitialize, "[\]]+\z", 3)
    If @error Then ; no initilizer
        Return SetError(1, 0, 0)
    EndIf

    Local $iDims = StringLen($aDims[0])
    Local $sArr = "$aRet"
    For $i = 1 To $iDims
        $sArr &= "[]"
    Next

    Local $aRet
    Execute("Dim " & $sArr & " = " & $sInitialize)
    Return $aRet
EndFunc

But Alas, no joy.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
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
Sign in to follow this  
Followers 0