Dictionary : Case Sensitive

is there a way to make the keys unique based on case? According to the LR, only non-ASCII characters (> 0x7F) are treated this way.
I need a dictionary that is insensitive regardless

myDict.value("ABC")=5
myDict.value("abc")=50

if I can’t do it with a dictionary, then I’ll have to resort to a slower array :frowning:

Which one?

sorry

Look at Xojo.Core.Dictionary, which supports it.

documentation is not complete enough to make any sense out of.
There seems to be no relationship to HASKEY for example.

Easiest hack is to generate a new framework dictionary from an empty json.

On my phone or I’d give you code. Check the docs though.

Also check MacOSLib for my case-sensitive Dictionary class. It will work independently.

You can also refer to the included case sensitive example in Xojo Framework/DictionaryExample

[quote=235386:@Dave S]documentation is not complete enough to make any sense out of.
There seems to be no relationship to HASKEY for example.[/quote]

[code]Dim myDict As New Xojo.Core.Dictionary()

myDict.value(“ABC”) = 5
myDict.value(“abc”) = 50

MsgBox(“Count: " + Str(myDict.Count) + EndOfLine + “HasKey(”“aBc””): " + Str(myDict.HasKey(“aBc”)))
// Count: 1
// HasKey(“aBc”): True

myDict.RemoveAll()
AddHandler myDict.CompareKeys, AddressOf myDict_CompareKeys // Makes it case-sensitive

myDict.value(“ABC”) = 5
myDict.value(“abc”) = 50

MsgBox(“Count: " + Str(myDict.Count) + EndOfLine + “HasKey(”“aBc””): " + Str(myDict.HasKey(“aBc”)))
// Count: 2
// HasKey(“aBc”): False

RemoveHandler myDict.CompareKeys, AddressOf myDict_CompareKeys[/code]

Function myDict_CompareKeys(d As Xojo.Core.Dictionary, lhs As Auto, rhs As Auto) As Integer Return CType(lhs, Text).Compare(rhs, Text.CompareCaseSensitive) End Function

Travis, I am sorry, but I do not see such example in the Framework folder. The only dictionary example in Language Features is very basic an does not seem to contain any case sensitive method.

@Dave S : Why not EncodeBase64 your keys ? That would make them case sensitive. A subclass seems easy to do.

A code example of what Kem was talking about:

Function CreateCaseSensitiveDictionary() As Xojo.Core.Dictionary
  return Xojo.Data.ParseJSON("{}")
End Function

// other place in code:

dim d as Xojo.Core.Dictionary = CreateCaseSensitiveDictionary
  
d.Value("name") = "John"
d.Value("Name") = "Jack"
  
Print d.Value("name")
Print d.Value("Name")

The output will be

John
Jack

Example Projects/Xojo Framework/DictionaryExample has a CaseSensitiveDictionary inside.

@Travis Hill , ah yes. That is nice. So just create a new class who’s super is xojo.Core.Dictionary, CaseSensitiveDictionary in the example. Then implement the CompareKeys event:

  Dim lhsText As Text = lhs
  Dim rhsText As Text = rhs
  
  Dim value As Integer
  value = lhsText.Compare(rhsText, Text.CompareCaseSensitive)
  
  Return value

and you’re done. That’s pretty easy!

I’ve seen this recommended elsewhere, but that technique won’t work since Base64 is itself case-sensitive.

Interestingly enough, the Normal Dictionary is fastest, Dictionary from JSON is next and the CaseSensitiveDictionary that overrides the Event is the slowest, by far. Now, in normal use, probably will not matter. I created 100,000 random keys. Then inserted into each dictionary type, then did a lookup of each dictionary type… all the exact same pre-generated keys.

Results:

Normal Dictionary (Classic Framework)
Assign Keys: 107.2373ms
Get Keys: 65.26952ms

Dictionary from JSON (New Framework)
Assign Keys: 241.1384ms
Get Keys: 196.7326ms

CaseSensitiveDictionary (New Framework using Events)
Assign Keys: 590.029ms
Get Keys: 472.2623ms

Since my dictionary only has 128 entries… I found it just as fast to do FOR EACH

Thanks

[quote=235410:@Dave S]Since my dictionary only has 128 entries… I found it just as fast to do FOR EACH

Thanks[/quote]

It’d be interesting to see what a “general” threshold is for when to use an array vs. dictionary

What about simply uppercase the keys ?

because I needed the key of “abc” to not be the same as the key “ABC”

it is to map character “names” to the actual character in my PDF class.

add_to_charmap(&hc0,"À","À")
add_to_charmap(&he0,"à","à")

Uh… That’s how our JSONItem class does it.