Page 1 of 1

setkey deletes key if empty ??

Posted: 15 Sep 2016 08:39
by autocart
I made an ini-file template which I copy and then add values with setkey.
Sometimes it can also happen that the value being "set" is nothing (""), like this:

Code: Select all

setkey "", "Key", "Section", [ini-file];
In this case, I noticed, the command completely deletes the key.
Obviously this behaviour is on purpose, correct?
However, should it be on purpose like that? I think it would make (more) sense to leave the key with no value. Or not?

Re: setkey deletes key if empty ??

Posted: 15 Sep 2016 09:32
by highend
Then we'd need another command that deletes key(s).

Why don't you use a simple

Code: Select all

if ($value) { setkey $value, "Key", "Section", [ini-file]; }
instead (or a more sophisticated regex check)?

Input validation is always a good thing...

Re: setkey deletes key if empty ??

Posted: 15 Sep 2016 11:20
by autocart
highend wrote:Then we'd need another command that deletes key(s).

Why don't you use a simple

Code: Select all

if ($value) { setkey $value, "Key", "Section", [ini-file]; }
makes sense, thx

Re: setkey deletes key if empty ??

Posted: 15 Sep 2016 11:29
by admin
Pass flag 1 if you want to set an empty value:

Code: Select all

setkey $value, "Key", "Section", [ini-file], 1;

Re: setkey deletes key if empty ??

Posted: 15 Sep 2016 11:46
by PeterH
I think setkey/Syntax/value should document that "no value" means that the key will be deleted.

Maybe it would be better to have a flag meaning "Delete" (and No Value meaning to set to empty) - but I'm afraid that's too late now?

Re: setkey deletes key if empty ??

Posted: 15 Sep 2016 11:48
by admin
PeterH wrote:I think setkey/Syntax/value should document that "no value" means that the key will be deleted.

Maybe it would be better to have a flag meaning "Delete" (and No Value meaning to set to empty) - but I'm afraid that's too late now?
Just done. :)

I'm using standard Windows API for this. I agree the behavior is somewhat surprising. It goes back to the early nineties. Too late to touch it.