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

Sorting file names randomly



    Hi all :)

    A time ago (zsh-workers/19128) Bart explained me how to sort
randomly a group of files. Namely, the solution is this:

    array=(*(e:'reply=%0(l..$RANDOM)$REPLY:))
    array=(${(%)array)

    Just as a side note, this works because that %0... construct,
which is a prompt escape sequence, namely a conditional that says "if
more than 0 characters have been printed, print nothing, else print
the $RANDOM expansion). Obviously this is always false so the random
number is never printed but it is used for sorting.

    Well, the problem is that the above doesn't work if you have to
use more than one pattern, because the '(e' construct will affect
only the last element. Since I don't know how many elements will be
present, I cannot use an '(e' construct on each element. Moreover it
would be very messy.

    What I want to do is to generate (in an array) a list of files
sorted randomly, given some globbing patterns. Since the list can be
quite large, I think that doing the glob on the command line is not a
good idea, so I would call the function like:

    shuffle dir1/* dir2/* ...

    The globbing will be internal, so 'shuffle' is really an alias to
"noglob 'shuffle'".

    I've tried to use '$~' in the solution above (the '%0...' one),
but it doesn't work because although files in dir1 and files in dir2
are sorted randomly, dir1 files appear always before dir2 files. It
seems that the random number doesn't affect the sorting of pathnames
:?

    Any simple way of using the above solution for this new problem
or should I try a new solution? Any simple way of doing the random
sort on a group of patterns?

    Thanks a lot in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to...



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