AutoHotkey Community

It is currently May 27th, 2012, 5:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How to Delete 2D Array
PostPosted: December 30th, 2005, 7:12 pm 
Offline

Joined: November 18th, 2005, 11:16 pm
Posts: 77
Location: Southern USA
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2005, 7:17 pm 
Offline

Joined: November 18th, 2005, 11:16 pm
Posts: 77
Location: Southern USA
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2005, 9:12 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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 December 31st, 2005, 4:03 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2005, 9:21 pm 
Offline

Joined: November 18th, 2005, 11:16 pm
Posts: 77
Location: Southern USA
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?
Code:
varname =


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2005, 9:35 pm 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
hurricanedavid wrote:
:?: So does that mean if I do this to a variable, that it takes it out of memory?
Code:
varname =


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 =


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 30th, 2005, 10:14 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
"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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2006, 4:01 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2006, 4:33 am 
Offline

Joined: November 6th, 2004, 11:03 am
Posts: 170
Location: Salt Lake City, UT
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 :?:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2006, 7:08 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2006, 8:11 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2545
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...) ;) .


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2006, 5:05 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Clumsy Coding
PostPosted: January 2nd, 2006, 10:35 pm 
Offline

Joined: November 18th, 2005, 11:16 pm
Posts: 77
Location: Southern USA
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2006, 10:53 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Have you thought about using functions with local arrays?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2006, 11:34 pm 
Offline

Joined: November 18th, 2005, 11:16 pm
Posts: 77
Location: Southern USA
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 3rd, 2006, 12:08 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot], HotkeyStick, rbrtryn, XstatyK and 81 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group