I would like to create a dictionary variable for a vimL script. That dictionary will hold information related to a folder structure. However, I have a bit of hard time designing this variable and also I came across some difficulties while handling the information stored in that variable. Below is an example dictionary variable.
let g:myTree = {
\ 'base' : {
\ 'path' : '__base',
\ 'main' : {'path' : '__base/__main'},
\ 'config' : {
\ 'path' : '__base/__/config',
\ 'modules' : {'path' : '__base/__config/__modules'},
\ 'settings': {
\ 'path' : '__base/__config/__settings',
\ 'gui' : {
\ 'path' : '__base/__config/__settings/__gui',
\ 'linux' : {'path' : '__base/__config/__settings/__gui/__linux'},
\ },
\ },
\ },
\ },
\ }
The idea was to be able to access the data bits with the .
notation. For example g:myTree.base
should return __base
. However, I have noticed that I need an additional key path
in order to be able to package the nested information, so the access is done through g:myTree.base.path
for __base
.
The first questions would be:
Is there a way to avoid the use of path
and directly access the data in the following format:
g:myTree.base
for __base
g:myTree.base.main
for __base/__main
g:myTree.base.config.modules
for __base/__config/__modules
g:myTree.base.config.settings.gui.linux
for __base/__config/__settings/__gui/__linux
The second question would be:
How do we iterate and access (or modify) the data bits stored in the path
key throughout the whole depth of the dictionary. I have encountered a problem due to the fact that the path
key stores a string
value but the next key would be storing a dictionary value. This creates a variable mismatch when iterating through the dictionary. What would be the solution to this?
Thanks.
:h type()
could help.root
value of any nested dictionary without the need for an extra container, so that there is less typing. I wanted to be sure that there is no special method that will return the data from the top of each dictionary entry. Like for example:foo = {'_' : 'house', 'bar': 'apple'}
so thatfoo
will returnhouse
andfoo.bar
will returnapple
bar
is just as likely to be the top as_
.'_'
key with thehouse
value in there but you would not need to typefoo._
to access thehouse
but onlyfoo
will be enough.foo.P()
instead offoo.path
.