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

Re: Help with paths and file sorting

J wrote:
> Suppose you have an array "files" that contain the names of the files
> you want to sort.
> You can just go along the lines of
> tmp=\(\(${(pj:|:)files}\)\)
> files=(${~tmp}(<put here any sort qualifiers you want>))
> But there is a caveat :
> If one of the filenames have a character in it that can be interpreted
> as a pattern for globbing, it will, and that's not what you want. A
> solution to that is to quote the ${files} array with a (q) flag, but
> then spaces in filenames (or any other special char for that) would be
> quoted too in the ${tmp} array, meaning they won't match themselves
> because these quotes are not going to be removed by the ${~tmp}
> construct :/

You can get around this by using eval instead of ~.

eval files="(("${(j.|.)${(q)files}}")(<quals>))"

That's safe because the only part you're not directly in control of,
$files, is fully quoted.  It's probably best if <quals> includes N for
nullglob so that the resulting files array is empty if nothing matches.

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.


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