Dictionary Strangeness

  If keywords(index).HasKey(word) Then 
    k=keywords(index).value(word)
    words.append word:k
    If word="INTEGER" Then debug "HasKey "+word+":"+Str(k)
  Else
    words.append word:0
    For Each key As String In keywords(index).Keys
      If key=word Then debug "KY:"+key+"="+keywords(index).value(key)
    Next
  End If

this code is supposed to lookup “WORD” in the KEYWORDS dictionary and return a value of K
if the word is not in the dictionary is defaults to 0 [ie. after the ELSE]

HOWEVER… if is NEVER doing the before ELSE code, yet if the FOR EACH loop it is showing me the word I’m looking for!

This was working the other day… not sure what could have happened…

AND yes… there are NO whitespace characters …

replaceing the above code with

  
  k=0
  For Each key As String In keywords(index).Keys
    If key=word Then 
      k=keywords(index).value(key)
      exit for
    end if
  Next
  
  words.append word:k

DOES work… but defeats the purpose of a dictionary

Since a dictionary uses a hash mechanism, it is case sensitive, whereas string comparison is not.

That makes sense… confused as to what I changed… hmmm…

Thanks

Scratch that. HasKey is not case sensitive. Don’t remember where I got that notion. But there are some variations where dictionary hash and simple comparison differ.

But I’d look for some simpler explanation.

In your debug KY line, print the hex values of word and key.

String keys in a Dictionary are case-insensitive, but encodings will matter. If you try to use the same word with different encodings, I believe the lookup will fail.

BTW, once you get that part figured out, you might want to use Lookup instead.

k = keywords( index ).Lookup( word, 0 )
words.Append word:k

Not sure why… but some was UTF8 and some was something else… so a few encodings convert and it worked…

Thanks for the tip Kem