Clearing the contents of a dictionary

23 views
Skip to first unread message

Yegappan Lakshmanan

unread,
May 24, 2020, 8:16:00 PM5/24/20
to vim_dev
Hi,

To clear the contents of a list named "abc", we can use "unlet abc[:]".
It looks like a similar method for clearing the contents of a dictionary
is not available. We can set the dictionary variable to another empty
dictionary (but it is not the same). Am I missing something?

Thanks,
Yegappan

Tony Mechelynck

unread,
May 24, 2020, 8:44:51 PM5/24/20
to vim_dev
You mean, after :let xyz = {} the old keys and values still exist? I
wouldn't have expected that.

Oh, and what about
for k in keys(xyz) | exe 'unlet xyz.' .. k | endfor
?

Best regads,
Tony.

Yegappan Lakshmanan

unread,
May 24, 2020, 11:31:32 PM5/24/20
to vim_dev
Hi Tony,

On Sun, May 24, 2020 at 5:44 PM Tony Mechelynck <antoine.m...@gmail.com> wrote:
On Mon, May 25, 2020 at 2:15 AM Yegappan Lakshmanan <yega...@gmail.com> wrote:
>
> Hi,
>
> To clear the contents of a list named "abc", we can use "unlet abc[:]".
> It looks like a similar method for clearing the contents of a dictionary
> is not available. We can set the dictionary variable to another empty
> dictionary (but it is not the same). Am I missing something?
>
> Thanks,
> Yegappan

You mean, after :let xyz = {} the old keys and values still exist? I
wouldn't have expected that.


Consider the following function for clearing a List:

func ClearList(l)
    unlet a:l[:]
endfunc

let l = [1, 2, 3, 4]
call ClearList(l)
echo "List = ->" .. string(l) .. "<-"

After calling the ClearList() function, all the elements in the List
are cleared.

A similar function for clearing a Dict:

func ClearDict(d)
    for k in keys(a:d)
             unlet a:d[k]
    endfor
endfunc

let d = {'a' : 10, 'b' : 20}
call ClearDict(d)
echo "Dict = ->" .. string(d) .. "<-"

In this function, we have to iterate through all the keys and unlet each
item. The Dict cannot be cleared by simply using unlet or by assigning
the argument to an empty dictionary.
 
Regards,
Yegappan

Pavol Juhas

unread,
May 24, 2020, 11:37:00 PM5/24/20
to vim_dev
How about filter(d, “0”) ?
To my understanding it clears the dictionary in place.

Yegappan Lakshmanan

unread,
May 25, 2020, 2:27:37 AM5/25/20
to vim_dev
Hi,

On Sun, May 24, 2020 at 8:37 PM Pavol Juhas <pavol...@gmail.com> wrote:
How about  filter(d, “0”) ?
To my understanding it clears the dictionary in place.

Yes. Good idea. This does clear all the items in the dict. But for consistency
reasons, it will be useful to have unlet also support clearing all the items
in a Dict.

Thanks,
Yegappan

Bram Moolenaar

unread,
May 25, 2020, 4:35:37 PM5/25/20
to vim...@googlegroups.com, Yegappan Lakshmanan
The "unlet" command would be a weird way to clear a dict.
The suggestion to use filter() works well enough:

call filter(dict, 0)

Adding a new function isn't really needed, I would think clearing a
dictionary while keeping the identity is rare.

--
If Microsoft would build a car...
... You'd have to press the "Start" button to turn the engine off.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Andy Wokula

unread,
May 26, 2020, 1:39:00 PM5/26/20
to vim...@googlegroups.com
Am 25.05.2020 um 22:35 schrieb Bram Moolenaar:
> Yegappan wrote:
>
>> To clear the contents of a list named "abc", we can use "unlet abc[:]".
>> It looks like a similar method for clearing the contents of a dictionary
>> is not available. We can set the dictionary variable to another empty
>> dictionary (but it is not the same). Am I missing something?
>
> The "unlet" command would be a weird way to clear a dict.
> The suggestion to use filter() works well enough:
>
> call filter(dict, 0)
>
> Adding a new function isn't really needed, I would think clearing a
> dictionary while keeping the identity is rare.

:h filter()
already mentions this method.

But for 0, a string should be used, similar to searchpair()'s {skip} expression
(which does not accept a number):
:call filter(dict, "0")

--
Andy
Reply all
Reply to author
Forward
0 new messages