Page 1 of 1
Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 23 Mar 2024, 08:38
by oldbrother
My test code:
- test.zip
- (25.12 KiB) Downloaded 23 times
(I'm using geek's CJson 0.4.1 for AHK 1)
I have tried it in V2 with the same dada and geek's CJson for V2. Works fine.
Code: Select all
fileRead, data, data.txt
MyWords:=JSon.load(data)
NewWord :="AAA"
MyWords[NewWord] := "2" ;works
msgbox, % MyWords[NewWord]
MyWords.Delete(NewWord) ;does not work
msgbox, % MyWords[NewWord]
;
; cJson.ahk 0.4.1
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 23 Mar 2024, 09:08
by mikeyww
Hello,
This is not necessarily a bug-- I am not certain-- but something unexpected. If
Delete is a key in your array, then
word.Delete(NewWord) refers to your array property or element rather than referring to the Delete method.
Code: Select all
#Requires AutoHotkey v1
NewWord := "AAA"
word := {"AAA": 1, "Delete": "NOT A METHOD"}
MsgBox % word[NewWord]
word.Delete(NewWord)
MsgBox % word.Delete
MsgBox % word[NewWord]
For all types of objects, the notation
Object.LiteralKey can be used to access a property, array element or method, where LiteralKey is an identifier or integer and Object is any expression. Identifiers are unquoted strings which may consist of alphanumeric characters, underscore and, in [v1.1.09+], non-ASCII characters.
Source: Objects - Definition & Usage | AutoHotkey v1
Perhaps AHK should use the presence of parentheses to know that the statement uses a method.
I think that this is a bug; it appears that when the array contains a key called "Delete", the Delete method can no longer be used.
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 23 Mar 2024, 12:51
by oldbrother
I think that this is a bug; it appears that when the array contains a key called "Delete", the Delete method can no longer be used.
Thank you mikeyww! You are the best! I know lexikos posted "End of life v1.1" yesterday, but I really hope he can get this fixed. Cause I'm still a V1.1 guy
. I hope someone can bring this to his attention.
.
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 23 Mar 2024, 13:08
by oldbrother
I posted it in the bug report.
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 24 Mar 2024, 06:20
by oldbrother
You might have seen the response from Lexikos on the bug report board. I understand his point, but how can I remove key-value from the array? It's not practical to rebuild the array every time I need to remove a key-value pair.
Code: Select all
#Requires AutoHotkey v1
words := {"AAA": 1, "Delete": 2, "Add": 3, "Hello": 4, "Remove": 4}
NewWord := "AAA"
words[NewWord] := "5" ; Works
MsgBox % words[NewWord]
words.Delete(NewWord) ; Doesn't work
MsgBox % words[NewWord]
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 24 Mar 2024, 07:34
by mikeyww
You can do it in v2.
Code: Select all
#Requires AutoHotkey v2.0
word := Map()
For pair in StrSplit(FileRead('data.txt'), ',', '{}')
part := StrSplit(pair, ':', '" '), word[part[1]] := part[2]
NewWord := 'AAA'
MsgBox word[NewWord]
word.Delete(NewWord)
MsgBox word.Has(NewWord)
MsgBox word["Delete"]
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 24 Mar 2024, 12:04
by oldbrother
Thank you mikeyww! I add ‘K_’ to all the keys as a workaround, and it is working properly now.
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 24 Mar 2024, 12:10
by mikeyww
Sounds good. Actually, any prefix, suffix, etc. that prevents having the conflicting key will work.
Re: Object.Delete(Key) doesn't work in my case. Looks like a bug.
Posted: 29 Mar 2024, 01:19
by Chunjee
Good reminder and workaround. I think I ran into this once or twice