Re: Wish: opposite to split() ...

2007-10-20 Fir de Conversatie Andy Wokula

Ben Schmidt schrieb:
 Andy Wokula wrote:
 ... and I don't mean join().

 I'd like to have a function like split(), except for it should collect all
 the _matches_ in a list.  matchlist() would be a good name for it,
 unfortunately it's already taken.

 How would I do it now?
 
 A naive and inefficient but simple implementation:
 
 function ListOfMatches(subject,pattern)
   let l:matches = []
   let l:matchnum = 1
   let l:curmatch = matchstr(a:subject, a:pattern, 0, l:matchnum)
   while l:curmatch != 
   let l:matches = add(l:matches, l:curmatch)
   let l:matchnum = l:matchnum + 1
   let l:curmatch = matchstr(a:subject, a:pattern, 0, l:matchnum)
   endwhile
   return l:matches
 endfunction
 
 E.g.
 
 :echo ListOfMatches(abracadabra,a.)
 
 ['ab', 'ac', 'ad', 'ab']
 
 Enjoy!
 
 Ben.

Thx for your solution.  It has a small drawback though:
   
:echo ListOfMatches(abc, 'ab\|bc')
['ab', 'bc']

-- 
Andy

I already sent this message two days ago ... it got lost?

--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-19 Fir de Conversatie ap



On Oct 18, 10:29 pm, Andy Wokula [EMAIL PROTECTED] wrote:
 Tony Mechelynck schrieb:



  Andy Wokula wrote:
  ... and I don't mean join().

  I'd like to have a function like split(), except for it should collect all
  the _matches_ in a list.  matchlist() would be a good name for it,
  unfortunately it's already taken.

  How would I do it now?

   untested
   note: name, if not script-local, must start with an uppercase letter
  function Matchlist(string,pattern)
 let res = '['
 let s = a:string
 let p = '\(' . a:pattern . '\).*'
 while match(s,a:pattern) != -1
 let res .= substitute (s,p,submatch(1)) . ','
 let s = substitute(s,a:pattern,'')
 endwhile
 if res == '['
 return []
 else
 let res = substitute(res,'.$',']')
 return eval(res)
 endif
  endf

  Best regards,
  Tony.

 Thanks for the input, but I didn't get your function to work :-(
 Ok, for now I'll go with this:

 func! SplitMatch(str, pat)
 let matpos = match(a:str, a:pat)
 let reslist = []
 let mendp = 0match (after) end position
 while matpos = 0
 let mendp = matchend(a:str, a:pat, mendp)
 call add(reslist, strpart(a:str, matpos, mendp-matpos))
 let matpos = match(a:str, a:pat, mendp)
 endwhile
 return reslist
 endfunc

 --
 Andy


Try SplitMatch(aba,b*).
I would go for

let res= []
call substitute(str,pat,'\=add(res,submatch(0))','g')

1000 ways to kill a cow.

-ap


--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-19 Fir de Conversatie Ben Schmidt

 Still buggy:
 :echo SplitMatch(aba, .*)
 ['aba', '']

Looks OK to me.

Maybe you wanted

:echo SplitMatch(aba, '.\+')

Ben.




Send instant messages to your online friends http://au.messenger.yahoo.com 


--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-18 Fir de Conversatie Tony Mechelynck

Andy Wokula wrote:
 ... and I don't mean join().
 
 I'd like to have a function like split(), except for it should collect all
 the _matches_ in a list.  matchlist() would be a good name for it,
 unfortunately it's already taken.
 
 How would I do it now?
 

 untested
 note: name, if not script-local, must start with an uppercase letter
function Matchlist(string,pattern)
let res = '['
let s = a:string
let p = '\(' . a:pattern . '\).*'
while match(s,a:pattern) != -1
let res .= substitute (s,p,submatch(1)) . ','
let s = substitute(s,a:pattern,'')
endwhile
if res == '['
return []
else
let res = substitute(res,'.$',']')
return eval(res)
endif
endf



Best regards,
Tony.
-- 
Anthony's Law of Force:
Don't force it; get a larger hammer.

--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-18 Fir de Conversatie Ben Schmidt

Andy Wokula wrote:
 ... and I don't mean join().
 
 I'd like to have a function like split(), except for it should collect all
 the _matches_ in a list.  matchlist() would be a good name for it,
 unfortunately it's already taken.
 
 How would I do it now?

A naive and inefficient but simple implementation:

function ListOfMatches(subject,pattern)
let l:matches = []
let l:matchnum = 1
let l:curmatch = matchstr(a:subject, a:pattern, 0, l:matchnum)
while l:curmatch != 
let l:matches = add(l:matches, l:curmatch)
let l:matchnum = l:matchnum + 1
let l:curmatch = matchstr(a:subject, a:pattern, 0, l:matchnum)
endwhile
return l:matches
endfunction

E.g.

:echo ListOfMatches(abracadabra,a.)

['ab', 'ac', 'ad', 'ab']

Enjoy!

Ben.




Send instant messages to your online friends http://au.messenger.yahoo.com 


--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-18 Fir de Conversatie thomas


 :echo ListOfMatches(abracadabra,a.)

 ['ab', 'ac', 'ad', 'ab']

Not so efficient, but:

echo map(split('abracadabra', '\zea.'), 'matchstr(v:val, ''a.'')')

Maybe, one could call such a function scan().


--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---



Re: Wish: opposite to split() ...

2007-10-18 Fir de Conversatie Andy Wokula

Tony Mechelynck schrieb:
 Andy Wokula wrote:
 ... and I don't mean join().

 I'd like to have a function like split(), except for it should collect all
 the _matches_ in a list.  matchlist() would be a good name for it,
 unfortunately it's already taken.

 How would I do it now?

 
  untested
  note: name, if not script-local, must start with an uppercase letter
 function Matchlist(string,pattern)
   let res = '['
   let s = a:string
   let p = '\(' . a:pattern . '\).*'
   while match(s,a:pattern) != -1
   let res .= substitute (s,p,submatch(1)) . ','
   let s = substitute(s,a:pattern,'')
   endwhile
   if res == '['
   return []
   else
   let res = substitute(res,'.$',']')
   return eval(res)
   endif
 endf
 
 Best regards,
 Tony.

Thanks for the input, but I didn't get your function to work :-(
Ok, for now I'll go with this:

func! SplitMatch(str, pat)
let matpos = match(a:str, a:pat)
let reslist = []
let mendp = 0match (after) end position
while matpos = 0
let mendp = matchend(a:str, a:pat, mendp)
call add(reslist, strpart(a:str, matpos, mendp-matpos))
let matpos = match(a:str, a:pat, mendp)
endwhile
return reslist
endfunc

-- 
Andy

--~--~-~--~~~---~--~~
You received this message from the vim_dev maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---