Wednesday, January 26, 2011

Re: How calling function from filter(dictionary)?

Reply to message «Re: How calling function from filter(dictionary)?»,
sent 20:00:05 26 January 2011, Wednesday
by sgp:

> 1. Why does Compare() stop on error (case dC) in spite of the catch
> statement?

Looks like a vim bug, now posting it to vim-dev.

> 2. How can I test the type of tgt without resorting to catch? I
> just want to tell a string from a List.

if type(a:val)==type("")
return a:val=~?re
elseif type(a:val)==type([])
" your cycle here
endif


Original message:
> John Tobin wrote:
> > On 26 January 2011 13:51, sgp <acs322000@yahoo.com> wrote:
> >> How can I call a function from filter(dictionary)?
> >>
> >> This calls Compare() which then complains about val and tgt being
> >> undefined
> >>
> >> let dic=filter(copy(self),'v:key =~ "^pre" ?0 :
> >> call("Compare",[v:val, "'.tgt.'"])')
> >>
> >> function! LXcompare(val,tgt) " val can be string or List
> >>
> >> echo tgt
> >
> > echo val
> >
> > You need to use a:tgt and a:val. See :help a:var for details.
> >
> >> return 0
> >>
> >> endfunction
> >>
> >> TIA
> >>
> >> --
> >> You received this message from the "vim_use" maillist.
> >> Do not top-post! Type your reply below the text you are replying to.
> >> For more information, visit http://www.vim.org/maillist.php
>
> Sorry, I'm the OP and gave an incomplete example. Below is a better
> example, which should filter a dict by item value. A dict item value can
> be either a single string or a list of strings. Return 1 when the single
> string or one of the list item strings matches argument tgt.
>
> Two questions:
> 1. Why does Compare() stop on error (case dC) in spite of the catch
> statement? 2. How can I test the type of tgt without resorting to catch? I
> just want to tell a string from a List.
>
> let d1 = {1:'A', 2:['B','C']}
> function! Compare(val,tgt)
> let re='^'.a:tgt.'$'
> try | return a:val =~? re " a:val is a string
> catch
> "catch /^Vim\%((\a\+)\)\=:E691/ " catch Can only compare List with List
> for v in a:val
> if v =~? re | return 1 | endif
> endfor
> endtry
> return 0
> endfunction
> let dA = {1:'A'}
> echo filter(copy(dA),'call("Compare",[v:val, "A"])')
> let dC = {1:'A', 2:['B','C']}
> " should echo {2:['B','C']} but it stops on error, why?
> echo filter(copy(dC),'call("Compare",[v:val, "C"])')

No comments: