Good point. Consider the following:
FileKey = 'C:\config.sys'
zFiles[FileKey] = "3C13E7A0-D889B378-9D732B77-3FC10261"
z=zFiles[FileKey]
bExist = MapKeyExist(zFiles,FileKey)
; bExist is set to 1 as expected.
In the above example, bExist is set to 1 which indicates that the key does exist in the associative array. That is why the exact contents of the variable in both lines are important and an example that reproduces the problem is needed.
No, a NULL value could not produce the error.
The only way we have found to produce the behavior descript by the OP is to do something like the following:
key = '1'
map[key] = 'abc'
ErrorMode(@Off)
value = map[key]
bTest = MapKeyExist(map, key)
The reason the above produces behavior like that described by the OP is that the line map[key] = 'abc' doesn't create a WIL map. It creates a regular WIL array and the ErrorMode(@Off) line suppress the error generated by MapKeyExist.
There are several ways to avoid the above problem. You can create an empty map before assigning any values to the map. For example,
map = MapCreate()
map['1'] = 'abc'
bTest = MapKeyExist(map, '1')
correctly creates a WIL map that contains the key value "1".
Another way to prevent the problem is to make sure the key that creates the map cannot be converted to a number. For example,
map['not all digits'] = 'abc'
bTest = MapKeyExist(map, 'not all digits')
This behavior is also covered in the Consolidated WIL Help file.
BoxOpen(`Example`,``)
zFilesFN=DirScript():`Files.txt`
zKeysFN=DirScript():`Keys.txt`
If FileExist(zFilesFN)
mFiles=MapFileGetCsv(zFilesFN,@TAB)
Else
mFiles=MapCreate()
EndIf
If FileExist(zKeysFN)
mKeys=MapFileGetCsv(zKeysFN,@TAB)
Else
mKeys=MapCreate()
EndIf
zFolder=`C:\Windows\`
lFiles=``
If DirExist(zFolder)
BoxText(`Getting list of Files ...`)
lFiles=FileItemize(zFolder:`*.*`)
If lFiles!=``
For x=1 to ItemCount(lFiles,@TAB)
zFile=ItemExtract(x,lFiles,@TAB)
zFullName=zFolder:zFile
zFileInfos=FileInfoToArray(zFullName,1|2)
zFileSize=zFileInfos[1,1]
zModifiedDate=StrReplace(zFileInfos[1,2],`:`,``)
zCreatedDate=StrReplace(zFileInfos[1,4],`:`,``)
zFile=zFullName:`\`:zFileSize:`\`:zModifiedDate:`\`:zCreatedDate
If MapKeyExist(mFiles,zFile)
BoxText(zFile:@CR:`Map exists`)
Else
ErrorMode(@OFF)
Message(``,mFiles[zFile])
ErrorMode(@CANCEL)
zDigest=FileDigest(zFullName,`MD5`,1)
mFiles[zFile]=zDigest
If MapKeyExist(mKeys,zDigest)
BoxText(zFile:@CR:`Map not exists`:@CR:`Map exists`)
Else
BoxText(zFile:@CR:`Map not exists`:@CR:`Map not exists`)
mKeys[zDigest]=zFile
EndIf
EndIf
Next x
EndIf
EndIf
Rc=MapFilePutCsv(zFilesFN,mFiles,@TAB)
Rc=MapFilePutCsv(zKeysFN,mKeys,@TAB)
This gives me the error.
It would be negligent to not offer a simple one-line workaround until the next release.
strKey = 'key'
strPair = 'key,value'
; Unicode version of key.
strwKey = ChrStringToUnicode(strKey)
; Test map
aMap = MapCreate(strPair)
; The workaround -> force Unicode to ANSI string conversion.
strLook = ChrUnicodeToString(strwKey)
; Now the test.
bResult = MapKeyExist(aMap, strLook)
if bResult then Message('Map Workaround', 'Works!!')
else Message('Map Workaround', 'Is a miserable failure!!')