Jump to content

Anonymous arrays?


Recommended Posts

In languages like Perl you can create arrays on the fly, so that you can say things like:

myFunc(['a','b','c'])

which saves you having to define the array beforehand if you are just going to use it once.

Does AutoIt3 have a similar feature?

Link to comment
Share on other sites

If the function dont use ByRef, you can do that.

But that depends on the funtion, if you try to _arraydisplay([0,0,0]) wont work. Please check the help file for more information.

You can help! Donate to AutoIt! or, visit ClimatePREDICTION.netMy posts:Travian Bot Example (100+ servers) BETAHow to Host you code/app for free! (unlimited team number) (Public or Private)"Sir, we're surrounded!" "Excellent. We can attack in any direction!"
Link to comment
Share on other sites

If the function dont use ByRef, you can do that.

But that depends on the funtion, if you try to _arraydisplay([0,0,0]) wont work. Please check the help file for more information.

In a word, no. AutoIt does not have anonymous arrays. There used to be a function _ArrayCreate() in the Array.au3 UDF, but it is no longer there. With it, you could do something like this:
_ArrayDisplay(_ArrayCreate(0,1,2))

It would take much to code your own _ArrayCreate() to do 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

In a word, no. AutoIt does not have anonymous arrays. There used to be a function _ArrayCreate() in the Array.au3 UDF, but it is no longer there. With it, you could do something like this:

_ArrayDisplay(_ArrayCreate(0,1,2))

It would take much to code your own _ArrayCreate() to do that.

;)

I noticed that, why did they remove _ArrayCreate()?

-Joscpe

Link to comment
Share on other sites

I noticed that, why did they remove _ArrayCreate()?

I don't know, they probably decided it wasn't worth bloating the Array.au3 UDF with a function that only saves you one line of native script (DIM/Local/Global).

;)

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

Thanks for the good responses.

It seems like if enough people actually wanted this feature it wouldn't take much to add some syntactic sugar to the compiler.

But you're right, it is just because I am lazy and want to type in one line what I could in two.

Link to comment
Share on other sites

Actually _ArrayCreate() is still in the Array.au3 file and is still usable. You can create arrays with up to 21 elements with it, you simply won't see it on the list of auto-complete options. To prove a point here is an example.

#include <Array.au3>

ConsoleWrite("Array length is: " & _ArrayGetLength(_ArrayCreate(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)) & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayGetLength
; Description ...: Gets length of a given array
; Syntax.........: _ArrayGetlength(ByRef $avArray, $iBase)
; Parameters ....: $avArray    - Array to get length of
; Return values .: Success - Returns length of the array
;                  Failure - 0, sets @error:
;                  |1 - $avArray is not an array
;                  |2 - $avArray has too many dimensions (only up to 2D supported)
;                  |3 - Array not set to base 1 or 0
; Author ........: Michael (dbzfanatic)
; Modified.......: 
; Remarks .......:
; Related .......: UBound
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _ArrayGetLength($avArray,$iBase = 1)
    Local $iDimension = UBound($avArray, 0), $iUBound = UBound($avArray, 1) - 1, $iSubMax = UBound($avArray, 2) - 1, $i
    If IsArray($avArray) Then ;check to see if variable is an array
        If $iDimension > 2 Then
            Return SetError(2, 0, 0); sets @error = 2, array too large
        EndIf
        $i = UBound($avArray) ;get length of the array
        If $iBase = 1 Then
            Return $i ;return the length of the array
        ElseIf $iBase = 0 Then
            Return $i - 1
        Else
            Return SetError(3,0,0)
        EndIf
    Else
        Return SetError(1,0,0) ;set error code to 1, means variable isn't an array
        ConsoleWrite(@ScriptFullPath & "(" & @ScriptLineNumber & ") : ==> _ArrayGetLength used with non-array variable.:" & @CRLF)
    EndIf
EndFunc   ;==>_ArrayGetLength

and the output to the console is

>"C:\Program Files\autoit3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\Owner\Desktop\proof.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

+>03:24:33 Starting AutoIt3Wrapper v.1.10.1.8 Environment(Language:0409 Keyboard:00000409 OS:WIN_VISTA/Service Pack 1 CPU:X86)

>Running AU3Check (1.54.13.0) from:C:\Program Files\AutoIt3

+>03:24:33 AU3Check ended.rc:0

>Running:(3.2.12.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Users\Owner\Desktop\proof.au3"

Array length is: 21

+>03:24:33 AutoIT3.exe ended.rc:0

>Exit code: 0 Time: 1.673

Edited by dbzfanatic
Link to comment
Share on other sites

I find it easier to create arrays like so:

#include <Array.au3>
Dim $array[10]

$array[0] = "Something"
$array[1] = "Something Else"
$array[2] = "Smith"
$array[3] = "AutoIt"
$array[4] = "Richard"
$array[5] = "Paul"
$array[6] = "George"
$array[7] = "Brett"
$array[8] = "Ron"
$array[9] = "Ian"

_ArrayDisplay ($array)

;But say i needed to add some other infomation into the array.
;For that we can use ReDim, so here we go.

ReDim $array[16]


$array[10] = "Big"
$array[11] = "Things"
$array[12] = "Come"
$array[13] = "In"
$array[14] = "Big"
$array[15] = "Boxes"

_ArrayDisplay ($array)

Remember that the index for the arrays start at 0.

Cheers,

Brett

EDIT: @dbzfanatic: having a look at your code, what is the difference between that and normal UBound()? Just wondering ;)

Edited by BrettF
Link to comment
Share on other sites

Actually _ArrayCreate() is still in the Array.au3 file and is still usable. You can create arrays with up to 21 elements with it, you simply won't see it on the list of auto-complete options.

But that is a temporary condition, _ArrayCreate() will go away soon, don't make a habit of using it. From the 3.2.12.1 Prod Array.au3 (same in 3.2.13.7 Beta):

; #NO_DOC_FUNCTION# ================================================

; Not documented - function(s) no longer needed, will be worked out of the file at a later date

; ================================================================

;_ArrayCreate

; ================================================================

;)

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