AutoHotkey Community

It is currently May 27th, 2012, 1:17 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: arrays & hashes
PostPosted: April 27th, 2005, 2:56 am 
Hi!

While we have simulated arrays today are there any plans for real arrays? Also I would love to perl type hash arrays where the the key can be any text string. Sorry if either of these has been posted before.

Thanks!

Dave Venus


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2005, 9:17 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Real arrays are planned eventually, but it might be a while. I think you can already do hash arrays except that certain characters are illegal, so the hash string would first have to be cleansed of these illegal characters. Example:

StringReplace, PersonName, PersonName, -,, All ; Remove dashes.
StringReplace, PersonName, PersonName, ',, All ; Remove apostrophes.
StringReplace, PersonName, PersonName. ',, All ; Remove periods.
DateOfBirth%PersonName% := DOB


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2005, 1:23 am 
Offline

Joined: July 20th, 2005, 4:49 am
Posts: 65
Here's a really simple way to do lookups by name

Code:
/*
A list of items to hash, might be read from a file or somewhere else

   fruit => apple
   string+-with.,illegalvarnamechars => banana
   vegetable => carrot
*/

;create a unique valid var name for the key
;the []'s are just to make it look like a hash/array lookup

key := Hash("fruit")
items[%key%] = apple
key := Hash("text+-with.,illegalchars")
items[%key%] = banana
key := Hash("vegetable")
items[%key%] = carrot

;find the item

key := Hash("text+-with.,illegalchars")
value := items[%key%]
msgbox %value%

return

; generate uniq ids
; this function returns a reversible representation of a string that
; obeys ahk's rules for variable names

Hash(k)
{
   loop, parse, k,
   {
      a := Asc(A_LoopField)
      hashed_key = %hashed_key%$%a%
   }
   return %hashed_key%
}


This way of working is dependant upon the max length of ahk variables and upon the way ahk looks up variable names, which at one point was very slow.

See http://www.autohotkey.com/forum/viewtopic.php?t=3004&postdays=0&postorder=asc&highlight=hash&start=0
for more info

I'm not sure what resulted from this discusison. Hopefully this kind of code
works much quicker than it used to.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2005, 1:33 am 
Offline

Joined: July 20th, 2005, 4:49 am
Posts: 65
Also, If key length is a problem you could use a hex representation which would remove the need for a delimiter and keep size to a constant of two chars.

eg Hello becomes 48656C6C6F


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2005, 1:39 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Thanks for posting your lookup/naming strategy. In addition to being convenient and useful, it should perform well because Laszlo provided a double-binary-search algorithm that makes variable lookups on things like Array%i% much faster than they were in really old versions of AutoHotkey.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2005, 1:54 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
For a simple hash function you could use CRC-32. Converting text with special ANSI characters to a stream of hex digits can be done with the function String2Hex(), in the CRC posting.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 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