Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Dictionary (Hash,Associative Array,map...) using perlahk


  • Please log in to reply
1 reply to this topic
MisterW
  • Members
  • 65 posts
  • Last active: Jun 18 2007 11:14 AM
  • Joined: 20 Jul 2005
Here's a pretty basic implementation of a Dictionary (also called hash) using thomasl's nifty perlahk dll
see:
http://www.autohotke...opic.php?t=4593
for discussion and https://ahknet.autoh...asl/perlahk.zip
for the dll code

I'm constantly frustrated by ahk's support for complex data structures so I'm hoping this might be a path to an easy solution.

Presumably some of the issues with this solution (including speed) could be fixed by adding more functions to the dll? (ie: direct support for HV's)

Perl_Module=0

perlinit()
{
	global Perl_Module
	Perl_Module := DLLCall("LoadLibrary","str","perlahk.dll","UInt")
	DllCall("perlahk\PerlInit","int",0)
}

perlexit()
{
	global Perl_Module
	DllCall("perlahk\PerlExit")
	DllCall("FreeLibrary","UInt",PERL_Module)
}

; run some perl code
perl(code) {
	DllCall("perlahk\PerlEval","str",code)
}

; here string is a variable defined using VarSetCapacity
perlstr(name,ByRef string)
{
	DllCall("perlahk\PerlGetStr","str",name,"str",string)
}

; get an integer
perlint(name)
{
	return DllCall("perlahk\PerlGetInt","str",name)
}


CreateMap(mapname)
{
	perl("%" . mapname . " = ();")
}

Clearmap(mapname)
{
	CreateMap(mapname)
}

Fetch(mapname,key)
{
	perl("$courier = $" . mapname . "{" . key . "};")
	perl("$package_size = length($courier);")
	size := perlint("package_size")
	VarSetCapacity(str,size + 5)
	perlstr("courier",str)
	return str
}

; this assumes string doesn't contain a ' char as it is used as a delimiter
; find a better solution
Store(mapname,key,value)
{
	perl("$" . mapname . "{" . key . "} = '" . value . "';")
}

;----------------------------------

/*
CreateMap(mapname)
Fetch(mapname,key)
Store(mapname,key,value)
Clearmap(mapname)
*/

perlinit()

CreateMap("skivvy")
Store("skivvy","Murray","Red")
Store("skivvy","Anthony","Blue")
Store("skivvy","Greg","Yellow")
Store("skivvy","Geoff","Purple")

Skivy_Color := Fetch("skivvy","Greg")

msgbox Greg wears a %Skivy_Color% skivvy

perlexit()


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Nice-looking data structure and presentation. This should help others with some of the AHK limitations you mentioned.