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

Re: Sorting file names randomly



    Hi all :) and sorry for self-replying...

 * DervishD <zsh@xxxxxxxxxxxx> dixit:
>     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)
[...]
>     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.

    I've written a not very good solution that at least works:

function shuffle () {

    emulate -L zsh 
    
    setopt nullglob globdots rcexpandparam
    
    RANDOM=`date +%s`
    [[ $# -eq 0 ]] && set '*'

    reply=($~*(e:'REPLY="${(l.5..0.)RANDOM} $REPLY"':))
    reply=(${(o)reply})
    reply=(${reply/#????? /})

    print -l $reply

    return 0
}
alias shuffle="noglob 'shuffle'"

    The function returns the list in the 'reply' array parameter, and
prints it on stdout.

    If anybody can make it better/shorter, suggestions are welcome ;)
The three array assignments can be probably shortened.

    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