Sign in to follow this  
Followers 0
muzle6074

How do I create a dynamic array?

8 posts in this topic

Hi all,

Long time no scripting, how can I declare a dynamic array in au3? By this I mean an array that I can just keep re-adding values to without changing the size of the array.

Regards

muzle6074

Share this post


Link to post
Share on other sites



To my knowledge dynamic arrays are array that can be resized but that doiesn't happen automatically.

You either declare an Array that is large enough to hold all instances or you redim the array when you need more.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

@muzle6074

if you want to add something to the array you have to change it's size but it doesn't mean it can't be dynamically done.

#include <Array.au3>
Dim $yourAr

For $i = 0 To 5
    $retNew = InputBox("Extend array", "Add something new to this array:")
    If IsArray($yourAr) = 1 Then
        $Bound = UBound($yourAr)
        ReDim $yourAr[$Bound+1]
        $yourAr[$Bound] = $retNew
    Else
        Dim $yourAr[1]
        $yourAr[0] = $retNew
    EndIf
Next

_ArrayDisplay($yourAr)

(E)Njoy :)

1 person likes this

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Share this post


Link to post
Share on other sites

muzle6074,

To the best of my knowledge, AutoIt arrays are inherently dynamic in the sense that the values within a declared array can be updated any time you wish to any value you require without problem, and without affecting the number of elements.

From the Help file:

Data types in Arrays

It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays:

$Array[0]=1

$Array[1]=true

$Array[2]="Text"

$Array[3]=$AnotherArray

This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array.

If you do need to change the number of elements, Dim or ReDim are the functions to use - the latter preserves existing values within surviving elements. Additionally, the include file Array.au3 contains a whole bunch of interesting functions to add and delete elements from within code, but the functions tend to be quite slow if used within loops because of the ReDim calls that they use.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you all for your input!

Share this post


Link to post
Share on other sites

Another way: use array alternatives (object) like i.e. System.Collections.ArrayList or Scripting.Dictionary.

So you can increase/decrease your array object without ReDim.

Here an example:

$ObjList = ObjCreate("System.Collections.ArrayList") ; create
$ObjList.Add($VALUE)      ; add value
$VALUE = $ObjList.Item($Index)     ; get value
$ObjList.Remove($VALUE)   ; delete by name
$ObjList.RemoveAt($Index) ; delete by index
$Array = $ObjList.ToArray ; list to array
; and more...

$oDICT = ObjCreate('Scripting.Dictionary')
$oDICT.Add($KEY, $VALUE)   ; add pair (key/value)
$VALUE = $oDICT.Item($KEY) ; get value
$oDICT.Item($KEY) = $VALUE ; set value
$oDICT.Remove($KEY)        ; delete key
$oDICT.RemoveAll           ; delete all keys
; and more...
2 people like this

Best Regards BugFix  

Share this post


Link to post
Share on other sites

Great. ArrayList is so much easier to use. But any idea how to call static methods like IndexOf() ?

Another way: use array alternatives (object) like i.e. System.Collections.ArrayList or Scripting.Dictionary.
So you can increase/decrease your array object without ReDim.
Here an example:

$ObjList = ObjCreate("System.Collections.ArrayList") ; create
$ObjList.Add($VALUE)      ; add value
$VALUE = $ObjList.Item($Index)     ; get value
$ObjList.Remove($VALUE)   ; delete by name
$ObjList.RemoveAt($Index) ; delete by index
$Array = $ObjList.ToArray ; list to array
; and more...

$oDICT = ObjCreate('Scripting.Dictionary')
$oDICT.Add($KEY, $VALUE)   ; add pair (key/value)
$VALUE = $oDICT.Item($KEY) ; get value
$oDICT.Item($KEY) = $VALUE ; set value
$oDICT.Remove($KEY)        ; delete key
$oDICT.RemoveAll           ; delete all keys
; and more...

Share this post


Link to post
Share on other sites

nazikus,

Welcome to the AutoIt forums. :)

In future, please do not necro-post in threads this old - that functionality has already been incorporated into AutoIt. Look for the Map datatype in the latest Beta versions. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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