Jump to content

Creating empty Dynamic Array. Easy Question. How to?


 Share

Recommended Posts

Hey, this is a really stupid question, but bear with me.

How do I create a empty Dynamic Array?

The only way I get them to work is by creating an array with something already in it.

Ex (DOES NOT WORK):

#include <Array.au3>

Dim $avArray

_ArrayAdd( $avArray,"Brian")

I would like to use the above, but it doesn't seem to work when the array is empty.

Ex (DOES WORK)

#include <Array.au3>

Dim $avArray[1]

_ArrayAdd( $avArray,"Brian")

This one works, but I have to manually remove the first blank entry I created it with.
Link to comment
Share on other sites

Hi,

What you mean by "work/not work"?

To create an empty array, just do that:

Dim $Array[1]oÝ÷ Ù:(ëÚç­¢Øb±ªëk(î²ÚÞv)¢µ©Ýi×h®ël
ëk wöð«i®åzl"¶ajÛhi×n²)ඬ~éܶØ^²Úâ
7é«À®¶²ë-mæ§jºÚÉ«­¢+ÙI¥´ÀÌØíÉÉåmU½Õ¹ ÀÌØíÉÉ䤬Åt(ÀÌØíÉÉåmU½Õ¹ ÀÌØíÉÉ䤴ÅtôÅÕ½Ðí É¥¸ÅÕ½Ðì

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

I was just wondering if there was any way to create an empty array with no set size.

Like $array[1] has a set size of one. Of course I can still use _ArrayAdd, and add something to it, but say I have something that goes and deletes everything in the array...

For $i=0 To UBound($array)

_ArrayPop($array)

$i++

Next

Then when it's done it becomes $array instead of $array[]..

..so if I want to add something to it again, because we've deleted all of the elements, now we can't add something to it, because it's not an array anymore, it's like a regular variable now (I may be wrong on this part, but from my experience, this is what I came to conclude to)

I know there's ways around it, I'm just wondering if it's possible to have an 'empty' array.

Also side note: Never did get this.... What does 'Dim' do? I can still declare an array without the 'Dim'

Link to comment
Share on other sites

To explain my above work/doesn't work scenario...

Try this in auto it..

#include <Array.au3>
Dim $avArray
_ArrayAdd( $avArray,"Brian")
Msgbox(0,"",UBound($avArray))

And then try this...

#include <Array.au3>
Dim $avArray[1]
_ArrayAdd( $avArray,"Brian")
Msgbox(0,"",UBound($avArray))

See how the first one gives you a result of "0" (doesn't add anything to the array like it's supposed to)

because I'm not allowed to declare $avArray[0], so I can only declare $avArray (which isn't in 'array' format)

And see how the second one gives me a result of 2 (works)

Again, I know there's ways around it, I'm just wondering if it's possible to have an 'empty' array. Ex: $avArray[0]

ps: What exactly does 'Dim' do? I can declare stuff without it (another stupid question).

Sry if this confuses you. I know how to work around it (think on my own), but I'm just trying to fully 'understand' it all.

Edited by CrewXp
Link to comment
Share on other sites

Write/ modify the _ArrayAdd() function to suit your needs , here's a quick n dirty example..

#include <Array.au3> ; for _ArrayDisplay
Dim $test

 _ArrayAddCreate($test, "What")
_ArrayDisplay($test, " 1st added")

_ArrayAddCreate($test, "Next")
_ArrayDisplay($test, " 2nd added")

Func _ArrayAddCreate(ByRef $avArray, $sValue)
    If IsArray($avArray) Then
        ReDim $avArray[UBound($avArray) + 1]
        $avArray[UBound($avArray) - 1] = $sValue
        SetError(0)
        Return 1
    ElseIf Not IsArray($avArray) Then
        Dim $avArray[1]
        $avArray[0] = $sValue
        Return 2
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc   ;==>_ArrayAddCreate

Cheers

Link to comment
Share on other sites

I'm just wondering if it's possible to have an 'empty' array

This is an empty array - Dim $Array[1], and that's it, $Array[0] it's should contain data (or the count of all elements), if you want to delete all elements in array, you will do that:

#include <Array.au3>

Dim $Array[3] = [2, 1, 2] ;Array with data

_ArrayDisplay($Array)

Dim $Array[1] ;And here it's become to empty array

_ArrayDisplay($Array)

So what you asking is not logical... why you need this?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Hey, this is a really stupid question, but bear with me.

How do I create a empty Dynamic Array?

The only way I get them to work is by creating an array with something already in it.

Ex (DOES NOT WORK):

I would like to use the above, but it doesn't seem to work when the array is empty.

Ex (DOES WORK)

This one works, but I have to manually remove the first blank entry I created it with.

I'm probably not qualified to speak on this topic since I'm so new to au3 and scripting in general. However the only time I've been able to do what you are talking about is to either declare the array as MSCreatoR said, or use _ArrayCreate(), or use another command that writes to an array. For example:

#include <array.au3>
Dim $avArray
$avArray = StringRegExp('Brian', '(.*\w)', 3)
_ArrayDisplay($avArray)

But as I said I'm not speaking from any real experience and have a limited knowledge and understanding. For those who may criticize my post and level of knowledge on such topics, please look up the word "limited" in the context of limited knowledge and understanding :).

Link to comment
Share on other sites

This is an empty array - Dim $Array[1], and that's it, $Array[0] it's should contain data (or the count of all elements), if you want to delete all elements in array, you will do that:

#include <Array.au3>

Dim $Array[3] = [2, 1, 2] ;Array with data

_ArrayDisplay($Array)

Dim $Array[1] ;And here it's become to empty array

_ArrayDisplay($Array)

So what you asking is not logical... why you need this?

Ah, you really didn't get it. I know, it's hard to understand what I'm saying.

Again, sorry if this sounds dumb to you. When you say it's 'not logical', it is to me. I like to understand things and see how they work.

#include <Array.au3>


Dim $Array[1];And here it's become to empty array
_ArrayAdd($Array,"Blah")

Msgbox(0,"",Ubound($Array))

See how it shows the array having two elements instead of just one (created an array, then added ONE entry). I'ld like to be able to do that, but ending up with just ONE result (the entry i put in). But from what I can see, apparently you can't in autoit (again, may be wrong..... that's why im asking)...

Unless you do another method of creating the array, like what ssubirias3 stated (Create the array with the variable already defined). Thx :)

Thanks everyone ;)

Link to comment
Share on other sites

from help file for Ubound

Remember that the value returned by UBound is one greater than the index of an array's last element!

as pointed out by MsCreator

Msgbox(0,"",Ubound($Array)-1)

_ArrayAdd leaves first element "0" empty for element count

you can add count with Ubound

$Array[0] = Ubound($Array)-1

also run the example in help file for _ArrayDisplay

it shows how arrays can be created with or without the first array element reserved for the count

#include <Array.au3>

Dim $Array[1]

_ArrayAdd($Array,"Blah")
_ArrayAdd($Array,"Blah")
_ArrayAdd($Array,"Blah")

Msgbox(0,"",Ubound($Array)-1)

$Array[0] = Ubound($Array)-1

Msgbox(0,"",$Array[0])

_ArrayDisplay($Array)
Edited by rover

I see fascists...

Link to comment
Share on other sites

thanks man, you're the bomb. Totally forgot Ubound was one higher. That makes everything different :)

Thought wrong the whole time. I could have went the whole time programming in autoit, thinking that, if I hadn't asked.

Thanks again ;)

Link to comment
Share on other sites

ps: What exactly does 'Dim' do? I can declare stuff without it (another stupid question).

CrewXp, I didn't see anyone answer your other question which from a noob's perspective is not at all a stupid question. And after reading the Help file on Dim/Global/Local, I still wasn't clear on why or when to use each one. So I came up with this demo script that helped me connect the dots. Hopefully other noobs, like myself who lack a background in programming, will find this helpful.

; ======================================================================
; Dim/Local/Global are the same if there are no functions (1 scope).
; If there are functions in the script,  then when inside the function 
; Local overwrites variables previously declared.  Once the script 
; resumes outside the function the variables are restored  to the values
; previously declared with Dim/Local/Global.
; ======================================================================

Global $Var1 = "green"; is read anywhere in the script
Dim $Var2 = "green", $Var4
Local $Var3, $Var5

MsgBox(0, "Var Test --  " & @ScriptLineNumber, "Global $Var1 = " & $Var1 & @CRLF & "Dim $Var2 = " & $Var2 _
         & @CRLF & "Local $Var3 = " & $Var3 & @CRLF & "Local $Var4 = " & $Var4 & @CRLF & "Local $Var5 = " & $Var5)
;some code here

_Somefunc()
Func _Somefunc()
    Local $Var1 = "red"
    Local $Var2 = "red"
    Local $Var3 = "red"
    Global $Var4 = "yellow"
    Dim $Var5 = "purple"

    MsgBox(0, "Var Test --  " & @ScriptLineNumber, "Global $Var1 = " & $Var1 & @CRLF & "Dim $Var2 = " & $Var2 _
             & @CRLF & "Local $Var3 = " & $Var3 & @CRLF & "Local $Var4 = " & $Var4 & @CRLF & "Local $Var5 = " & $Var5)
EndFunc   ;==>_Somefunc
MsgBox(0, "Var Test --  " & @ScriptLineNumber, "Global $Var1 = " & $Var1 & @CRLF & "Dim $Var2 = " & $Var2 _
         & @CRLF & "Local $Var3 = " & $Var3 & @CRLF & "Local $Var4 = " & $Var4 & @CRLF & "Local $Var5 = " & $Var5)
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...