How to make keys in objects case-sensitive?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

How to make keys in objects case-sensitive?

23 Jun 2014, 11:07

This code doesn't work the way I want:

words := {}
words["who"] := "an English pronoun"
words["WHO"] := "World Trade Organization"
msgbox % words["who"]
; shows "World Trade Organization" instead of "an English pronoun"

because the system seems to make no difference between "who" and "WHO" considering them to be one and the same key of the object. Is there any way to make the keys in objects case-sensitive?
User avatar
FanaticGuru
Posts: 1659
Joined: 30 Sep 2013, 22:25

Re: How to make keys in objects case-sensitive?

23 Jun 2014, 12:39

Plain vanilla objects do not support case sensitive keys but you can use a COM Dictionary object if you want case sensitive keys.

Code: Select all

words := ComObjCreate("Scripting.Dictionary")

words.item("who") := "an English pronoun"
words.item("WHO") := "World Trade Organization"
MsgBox % words.item("WHO")
The syntax is a little different but you can probably do what you want with a COM object.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: How to make keys in objects case-sensitive?

23 Jun 2014, 15:08

Re-post (guest posting messes the formatting):
You can wrap Scripting.Dictionary in a class to use it with AHK syntax:

Code: Select all

class Dict
{
	__New(p*) {
		this.Insert("_", ComObjCreate("Scripting.Dictionary"))
		Loop % p.MaxIndex()//2
			this[p[A_Index*2-1]] := p[A_Index*2]
	}

	__Call(m, p*) {
		if m in % "
		(LTrim Join,
		Add
		Exists
		Item,Items
		Key,Keys
		Remove,RemoveAll
		)"
			return (this._)[m](p*)
	}

	__Set(k, v, p*) {
		if (k = "base" || k = "__Class")
			goto bypass_set
		else if (k == "CompareMode")
			return this._[k] := v
		else if this.Exists(k)
			return this._.Item(k) := v
		else return this.Add(k, v)
		bypass_set:
	}

	__Get(k, p*) {
		if (k = "base" || k = "__Class")
			goto bypass_get
		else if k in Count,CompareMode
			return this._[k]
		else if this.Exists(k)
			return this.Item(k)
		bypass_get:
	}

	_NewEnum() {
		return new this.base.Enum(this)
	}

	class Enum
	{
		__New(obj) {
			this.obj := obj
			this.enum := obj._._NewEnum()
		}

		Next(ByRef k, ByRef v:="") {
			if (r:=this.enum.Next(k, v))
				v := (this.obj)[k]
			return r
		}
	}
}

d := new Dict("who", "an English pronoun", "WHO", "World Trade Orgaization")
d.Who := "Dr. Who"
for k, v in d
	MsgBox %k% = %v%
return
User avatar
tank
Posts: 3020
Joined: 28 Sep 2013, 22:15
Google: ttnnkkrr
GitHub: ttnnkkrr
Location: CarrolltonTX
Contact:

Re: How to make keys in objects case-sensitive?

23 Jun 2014, 15:28

deleted errored post
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
https://www.facebook.com/ahkscript.org
If you have forum suggestions please submit a
Check Out WebWriter
User avatar
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

Re: How to make keys in objects case-sensitive?

23 Jun 2014, 18:30

FanaticGuru wrote:...you can use a COM Dictionary object if you want case sensitive keys.

Code: Select all

words := ComObjCreate("Scripting.Dictionary")
words.item("who") := "an English pronoun"
words.item("WHO") := "World Trade Organization"
MsgBox % words.item("WHO")
- Thank you FanaticGuru for this code, but something is wrong. I tried to run it and it doesn't work.
FanaticGuru wrote:Plain vanilla objects do not support case sensitive keys
- What is a plain vanilla object?
User avatar
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

Re: How to make keys in objects case-sensitive?

23 Jun 2014, 18:35

Coco wrote:You can wrap Scripting.Dictionary in a class to use it with AHK syntax...
- Thank you Coco! Your script works just fine. I am studying it at the moment. Class is something that I know almost nothing about. It is both intimidating and very much attractive to me :)
User avatar
FanaticGuru
Posts: 1659
Joined: 30 Sep 2013, 22:25

Re: How to make keys in objects case-sensitive?

24 Jun 2014, 02:28

Benny-D wrote:
FanaticGuru wrote:...you can use a COM Dictionary object if you want case sensitive keys.

Code: Select all

words := ComObjCreate("Scripting.Dictionary")
words.item("who") := "an English pronoun"
words.item("WHO") := "World Trade Organization"
MsgBox % words.item("WHO")
- Thank you FanaticGuru for this code, but something is wrong. I tried to run it and it doesn't work.
FanaticGuru wrote:Plain vanilla objects do not support case sensitive keys
- What is a plain vanilla object?
Strange that the code does not work for you. It works fine for me. Do you have the newest version of AHK?
http://ahkscript.org/download/

As for plain vanilla object, that is not a technical term just me referring to the basic object like you were trying. COM objects are more like chocolate, not as common but a little more fancy and interesting.

Coco's class uses the same COM object but just adds some sugar to the syntax (that IS a semi-technical term). It is some pretty sweet sugar by the way. Thanks Coco!

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
Benny-D
Posts: 290
Joined: 12 Mar 2014, 10:09

Re: How to make keys in objects case-sensitive?

25 Jun 2014, 00:08

FanaticGuru wrote:Strange that the code does not work for you. It works fine for me. Do you have the newest version of AHK?
- Mine is 1.1.14.03
FanaticGuru wrote:As for plain vanilla object, that is not a technical term just me referring to the basic object like you were trying
- Ah, I see. Thank you.
FanaticGuru wrote:COM objects are more like chocolate, not as common but a little more fancy and interesting. Coco's class uses the same COM object but just adds some sugar to the syntax (that IS a semi-technical term). It is some pretty sweet sugar by the way
- :)

Return to “Ask For Help”

Who is online

Users browsing this forum: arczi_87, Chunjee, Google [Bot], inseption86, mikeyww, one1tick, XMCQCX and 54 guests