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
> 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
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}')
> 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
On Tue, November 10, 2009 12:19 pm, Andy Wokula wrote: > 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}))')
You don't need KeepVal(): :call map(copy(in_list), 'extend(out_dict, {v:val : 1})')
> On Tue, November 10, 2009 12:19 pm, Andy Wokula wrote: >> 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}))')
> You don't need KeepVal(): > :call map(copy(in_list), 'extend(out_dict, {v:val : 1})')
I just didn't like getting a list full of dict references. And the return value of map() will be less "junky".
I personally don't think it is a good idea to use map for iterating
over a list since it manipulates the list it is working on and returns
that transformed list.
Since vimscripts provides no high-order function to iterate over a
list without modifying it, what's wrong with a dull looking for-loop:
> I personally don't think it is a good idea to use map for iterating > over a list since it manipulates the list it is working on and returns > that transformed list.
> Since vimscripts provides no high-order function to iterate over a > list without modifying it, what's wrong with a dull looking for-loop:
> for k in list > let dict[k] = 1 > endfor
Nothing, it's the best option ;) But the OP already used that.