i want each lines of the output of a shell command to become a key of a
dict. I wote this code:
let t = {}
for k in split( system("echo foo; echo bar "), '\n' )
let t[k] = 1
endfor
but i don't like it: as the loop is here to populate the dictionnary, i
would like to use something more appropriate. In perl for exemple, the
map function enables you to write
my %a = map { $_ => 1 } split /\n/, qx< echo foo; echo bar>
vim has the equivalent of $_: it's called v:val, so i tried to use map
or filter with attempts looking like that:
let t = map( split( system("echo foo; echo bar "), '\n' ), { v:val : 1 } )
and yet i just wonder if it's possible.
regards,
marc
It is possible, the second argument to map() must be a string:
:h map()
:let t = map(["foo", "bar"], '{v:val : 1}')
--
Andy
On Mon, Nov 09, 2009 at 06:33:24PM +0100, Andy Wokula wrote:
> It is possible, the second argument to map() must be a string:
> :h map()
> :let t = map(["foo", "bar"], '{v:val : 1}')
i tried this solution but the result is
[{'foo': 1}, {'bar': 1}]
when i expect
{'foo': 1, 'bar': 1 }
regards
marc
Sorry, I didn't understand (dunno Perl).
I think you can't do it so nicely in Vim, but you can try the
following:
" helper to keep the list unchanged (not required):
func! KeepVal(_)
return v:val
endfunc
let in_list = ["foo", "bar"]
let out_dict = {}
call map(in_list, 'KeepVal(extend(out_dict, {v:val : 1}))')
" not so nice:
" - extra command for initialising out_dict
" - return value of map() is useless here
--
Andy
You don't need KeepVal():
:call map(copy(in_list), 'extend(out_dict, {v:val : 1})')
regards,
Christian
I just didn't like getting a list full of dict references. And the
return value of map() will be less "junky".
--
Andy
Okay, how about
:let @_=string(map(copy(in_list), 'extend(out_dict, {v:val : 1})'))
regards,
Christian
--
• EFI is this other Intel brain-damage (the first one being ACPI).
Torvalds, Linus (2006-07-24).
Nothing, it's the best option ;) But the OP already used that.
--
Andy
many thanks for you tries and replies. all of them where very
instructive.
i'll stick on the for-loop solution as it seems that functionnal
solutions are harder to read/debug in viml.
regards
marc
On Tue, Nov 10, 2009 at 10:11:46PM -0800, Hari Krishna Dara wrote:
> Since you seem to want a one liner, here is one:
>
> let t=eval('{'.join(map(split(system("echo foo; echo bar"), "\n" ),
> '"''".v:val."'': 1"'), ',').'}')
I want my code to be readable then short. it seems that viml isn't the
good langage for it. I stick on the for-loop :)
regards,
marc