How to empty an array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arathra
Posts: 23
Joined: 29 Aug 2016, 06:22

How to empty an array?

06 Oct 2016, 05:09

This should be easy but for the life of me I can't get it working.

How can I empty an array?

Code: Select all

global SearchWordsArray := Object()
; fill it with stuff, do stuff with it then... try this but it doesn't appear to work
SearchWordsArray.RemoveAll()
; then refill with other stuff and so on
Thanks for any help!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to empty an array?

06 Oct 2016, 05:28

There is no such built in method. Can't you just overwrite the array?
Guest

Re: How to empty an array?

06 Oct 2016, 06:08

Why would you need to have a builtin method? Just clear it :-)

Code: Select all

Array:=["a","b"]
MsgBox % ">" Array[1] "<"
Array:=[]
MsgBox % ">" Array[1] "<"
Array.Push("a")
MsgBox % ">" Array[1] "<"
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: How to empty an array?

06 Oct 2016, 06:14

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
arathra
Posts: 23
Joined: 29 Aug 2016, 06:22

Re: How to empty an array?

06 Oct 2016, 06:50

I didn't realise that wasn't a method - I found it in the archived forums but obviously made a mistake.

This appears to work then:

Code: Select all

global SearchWordsArray := Object()
; fill it with stuff, do stuff with it then... empty it thus:
SearchWordsArray := Object()
; then refill with other stuff and so on
Does this seem right?
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: How to empty an array?

06 Oct 2016, 07:28

To empty the entire SearchWordsArray, per the link that jNizM posted...

Code: Select all

SearchWordsArray := ""
arathra
Posts: 23
Joined: 29 Aug 2016, 06:22

Re: How to empty an array?

06 Oct 2016, 12:37

With this:

Code: Select all

global SearchWordsArray := Object()
; do some stuff
SearchWordsArray := ""
; load array
Then the array does not load and returns a length of 0.

This, however, loads and returns a length appropriate to what was inserted:

Code: Select all

global SearchWordsArray := Object()
; do some stuff
SearchWordsArray := Object()
; load array
I don't know if it's creating 2 SearchWordsArray objects or if redeclaring it is overwriting the first.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: How to empty an array?

06 Oct 2016, 13:17

Of course the first example doesn't work... you empty the array before trying to load it.
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to empty an array?

06 Oct 2016, 17:07

SearchWordsArray := "" does not necessarily "empty the array". It does not even necessarily free the array.

Code: Select all

SearchWordsArray := Object()
SecondReference := SearchWordsArray

if (SecondReference == SearchWordsArray)
    MsgBox Both variables contain the same array.

SearchWordsArray.Push(42)
MsgBox % SecondReference[1] ; 42

SearchWordsArray := ""
MsgBox % SecondReference[1] ; 42 -- Array is not empty!
An array object is just a value. If you store some other value in the variable (such as ""), it won't contain the array anymore.

There are only two ways to empty an array:
  1. Remove all of its items.
  2. Ensure there are no more references to the array; then it will be both emptied and deleted.
There are multiple ways to remove all items. The most efficient way can only be used if you know the range of values, and the array does not use objects as keys. If it is a simple array rather than an associative array, it is easy:

Code: Select all

if SearchWordsArray.MinIndex() != ""  ; Not empty.
    SearchWordsArray.Delete(SearchWordsArray.MinIndex(), SearchWordsArray.MaxIndex())
If you know the first array index is always 1,

Code: Select all

SearchWordsArray.Delete(1, SearchWordsArray.Length())
If it is an associative array using strings as keys, you need to choose a string last_key >= all existing keys.

Code: Select all

SearchWordsArray.Delete("", last_key)
If you only have one reference to the array, the easier solution is to replace it with a new array.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mstrauss2021, peter_ahk, Spawnova, william_ahk, zephyrus2706 and 359 guests