Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How do I find shortest match?



On Wed, 16 May 2012 15:18:31 -0400
TJ Luoma <luomat@xxxxxxxxx> wrote:
> I don't know what to do in the ??? to compare the filenames and 
> choose the shortest one.

  sz() { REPLY=${(l.4..0.)${#REPLY}} }
  print -l ${^MATCHES}(o+sz[1])

Note you'll need the MATCHES to be the actual path to the file since
glob qualifiers only work on real files.

There's a really nasty trick that doesn't require them to correspond to files, which you'll hate.  I think you can combine it into a single expression, but it's bad enough in two.

  local min
  min=(${${(o)${MATCHES//?/\?}}[1]})
  print -l ${MATCHES:#^${~min}}

I'm sure it's completely obvious what this is doing.  It's been posted before.

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author