setkey deletes key if empty ??

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
autocart
Posts: 1391
Joined: 26 Sep 2013 15:22

setkey deletes key if empty ??

Post 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?

highend
Posts: 14996
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: setkey deletes key if empty ??

Post 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...
One of my scripts helped you out? Please donate via Paypal

autocart
Posts: 1391
Joined: 26 Sep 2013 15:22

Re: setkey deletes key if empty ??

Post 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

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: setkey deletes key if empty ??

Post by admin »

Pass flag 1 if you want to set an empty value:

Code: Select all

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

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: setkey deletes key if empty ??

Post 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?

admin
Site Admin
Posts: 66678
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: setkey deletes key if empty ??

Post 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.

Post Reply