| View previous topic :: View next topic |
| Author |
Message |
hurricanedavid
Joined: 18 Nov 2005 Posts: 77 Location: Southern USA
|
Posted: Fri Dec 30, 2005 6:12 pm Post subject: How to Delete 2D Array |
|
|
I have a 2D array referenced like the following examples...
| Code: | array1_1
array1_2
array2_1
array2_2
| It could not be created with the StringSplit command. This is only a small example. My array is usually 1-5 levels deep in the first dimension, and then about 6 levels deep in the second dimension.
Is there an easy way to clear out the array? Or do I need to do a loop and iterate through each possibility? |
|
| Back to top |
|
 |
hurricanedavid
Joined: 18 Nov 2005 Posts: 77 Location: Southern USA
|
Posted: Fri Dec 30, 2005 6:17 pm Post subject: |
|
|
| Maybe I could use the ListVars command and then loop through its results, checking to see if the var name matched the arrays "prefix". Then I could do a delete if it matched. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Fri Dec 30, 2005 8:12 pm Post subject: |
|
|
Using ListVars is a very good idea, although not very fast, because it involves opening and closing a new window. Here is an example how to use it. | Code: | aa =
x = 1.0
array1_1 = a
array1_2 = bc
array2_1 = 12345
array2_2 = x ; example set of variables
ListVars
ControlGetText Text, Edit1, ahk_class AutoHotkey
Send !{F4}
Loop Parse, Text, `n
{
IfLess A_Index,4, Continue
StringSplit VarName, A_LoopField, [
StringLeft Name, VarName1, 5
If Name = array
%VarName1% =
}
MsgBox Done! |
Last edited by Laszlo on Sat Dec 31, 2005 3:03 am; edited 1 time in total |
|
| Back to top |
|
 |
hurricanedavid
Joined: 18 Nov 2005 Posts: 77 Location: Southern USA
|
Posted: Fri Dec 30, 2005 8:21 pm Post subject: |
|
|
oh, I didn't know that using ListVars would be so clunky. That won't work for my application. I guess I'll just do something else.
Maybe as I loop through the array, I can increment a counter to keep a total of elements.
So does that mean if I do this to a variable, that it takes it out of memory? |
|
| Back to top |
|
 |
evl
Joined: 24 Aug 2005 Posts: 1239
|
Posted: Fri Dec 30, 2005 8:35 pm Post subject: |
|
|
| hurricanedavid wrote: |
So does that mean if I do this to a variable, that it takes it out of memory? |
It just makes the contents of varname empty (from the manual):
| Quote: |
The memory occupied by a large variable can be freed by setting it equal to nothing, e.g. var =
|
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Fri Dec 30, 2005 9:14 pm Post subject: |
|
|
"Var =" removes the content, but the name of Var remains in the list of variables. If you use hundreds of thousands of them, the names might also be an issue (slows down variable reference, occupies memory). If you can put the processing in a function with local variables, they get removed when the function returns. There are some situations when you cannot. | The documentation wrote: | | Within a function, any dynamic variable reference such as Array%i% will always resolve to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it will be created as a local variable unless the assume-global mode is in effect. | That is, if you need a large number of global variables, you may not be able to use local arrays.
The ListVars solution is not really "clunky". It takes 10 lines of code and clears a few thousand entries under a second. |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2436
|
Posted: Sun Jan 01, 2006 3:01 am Post subject: |
|
|
| Laszlo wrote: | | The ListVars solution is not really "clunky". It takes 10 lines of code and clears a few thousand entries under a second. |
| Code: | ListVars
ControlGetText Text, Edit1, ahk_class AutoHotkey
Send !{F4}
|
Efficient, creative, handy maybe but certainly clunky... |
|
| Back to top |
|
 |
ranomore
Joined: 06 Nov 2004 Posts: 178 Location: Salt Lake City, UT
|
Posted: Sun Jan 01, 2006 3:33 am Post subject: |
|
|
Can ListVars be written to #ErrorStdOut ? or can you use the method the documentation offers to read it?
| Quote: | However, you can have the command prompt redirect the output to a file as in this example:
"C:\Program Files\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "My Script.ahk" >"Error Log.txt"
|
I dunno, untested, just a thought  |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Sun Jan 01, 2006 6:08 am Post subject: |
|
|
I don't know any way to write to ErrorStdOut from a script, but what would it be good for?
(For us, who learned English as a third language:
clunky = lacking grace in movement or posture; clumsy in form or manner; awkward) |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2436
|
Posted: Mon Jan 02, 2006 7:11 am Post subject: |
|
|
It would be handy to have an option for listvars that could return the result in a variable instead of displaying a window...
| Laszlo wrote: | (For us, who learned English as a third language:
clunky = lacking grace in movement or posture; clumsy in form or manner; awkward) | It's not a commonly used word for those of us that learned English as a first language either (at least not here...) . |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Mon Jan 02, 2006 4:05 pm Post subject: |
|
|
| corrupt wrote: | | It would be handy to have an option for listvars that could return the result in a variable instead of displaying a window | I posted a similar wish about KeyHistory. Only three readers said they would use it, so I don't see any chance to convince Chris to provide any of the three useful information pages (Key History – for dynamic hotstrings, timing/history dependent hotkeys, "press a key to continue", chording; List Vars – for deleting unneeded dynamic variables, on-line error checking; Recent Lines Executed – for debug information: user error messages with the location of the problem and with the last few lines executed). Most of the time there is a clunky workaround, though. |
|
| Back to top |
|
 |
hurricanedavid
Joined: 18 Nov 2005 Posts: 77 Location: Southern USA
|
Posted: Mon Jan 02, 2006 9:35 pm Post subject: Clumsy Coding |
|
|
| Laszlo wrote: | | clunky = lacking grace in movement or posture; clumsy in form or manner; awkward |
yes, clunky was what I meant. But "clumsy" is another word sometimes used for workarounds that are not all that smooth. To me, for this application, I don't want any windows to be open then immediately closed. That would be clunky. However, I probably would not hesitate to use that method in my personal-use scripts. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Mon Jan 02, 2006 9:53 pm Post subject: |
|
|
| Have you thought about using functions with local arrays? |
|
| Back to top |
|
 |
hurricanedavid
Joined: 18 Nov 2005 Posts: 77 Location: Southern USA
|
Posted: Mon Jan 02, 2006 10:34 pm Post subject: |
|
|
| Laszlo wrote: | | Have you thought about using functions with local arrays? |
How do you mean?
... as in passing the array "prefix" into the function, and letting the function iterate through the array, deleting everything? |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Mon Jan 02, 2006 11:08 pm Post subject: |
|
|
| No, the function should do all the array calculations with a local array. When you don't need the data any more, return, and if necessary, restart the function from an outer loop. All the previous local variables are automatically erased. |
|
| Back to top |
|
 |
|