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

Re: Sorting file names randomly



    Hi Bart :)

 * Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> dixit:
> On Jul 23,  9:42pm, DervishD wrote:
> }     shuffle dir1/* dir2/* ...
> There's no reason to noglob and alias this.  The space required to
> expand the glob on the command line is no worse than what you're
> doing inside the function anyway, and there aren't argument-size
> limits on calls to shell functions, only on external commands.

    I thought that command line size limits applied to shell
functions too and I wanted to avoid exceeding it. The expansion
inside the shell function is done in an assignment, not a call, so I
assumed that size constrains didn't apply.

    Thanks for the explanation, because I find very useful to be able
to do the glob in the command line, just in case I want to make sure
about what is being generated :)

> }     Any simple way of using the above solution for this new problem
> Not really; glob qualifiers aren't going to do it for you.

    OK. Thanks.
 
> } Any simple way of doing the random sort on a group of patterns?
> You'll have to first expand them and then sort the resulting array.

    That's, more or less, what I'm doing now in my function.
 
> }     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 following won't work in versions of zsh that lack the += assignment:

    Mine has, and I don't want this function to be portable, my only
aim is to make it work in my box ;)
 
>     function shuffle {
>       emulate -L zsh
>       integer i
>       reply=()
>       # set -- $~*   # uncomment to use with noglob alias
>       for ((i=1; i <= $#; ++i)) { reply[i*RANDOM/32768+1]+=($argv[i]) }
>       shift reply
>       print -l $reply
>     }

    Why is it better than my function? Appart from the fact that I
don't fully understand it ;) I don't see any advantage. What I'm
missing here? Maybe this is faster than doing the '(e' thing?
 
> So this is a true shuffle; for each "card" $argv[i], we insert it into
> the reply "deck" at a random position among the previous i-1 cards.

    Yes, I now see how it works.
 
> A more efficient way might be this:
> 
>     function shuffle {
>       emulate -L zsh
>       declare -A h
>       local +h -Z 5 RANDOM=$SECONDS
>       integer i
>       # set -- $~*   # uncomment to use with noglob alias
>       for ((i=1; i <= $#; ++i)) { h[$i.$RANDOM]=$argv[i] }
>       reply=( $h )
>       print -l $reply
>     }
> 
> This creates random but unique hash keys and then retrieves the shuffled
> values in one assignment; we don't care that the order of hash values is
> indeterminate, because we want it to be random!  The local RANDOM is
> there to force it to be zero-padded to 5 places, so all the hash keys
> are the same length; probably not essential.

    I understand this one better ;) That's another solution I thought
of, but I assumed that if I used associative arrays, the order of
elements would be the order in which they were inserted (which is
not, I've discovered right now). So my only 'safe bet' was to use a
normal array and using $RANDOM as the index, but that had another
problem: if I shuffle tree or four lines, I will have an array 2^16
items large, mostly empty.

    Anyway, the ordering of elements in an associative array is not
very random if $RANDOM is not included in the key, and I don't
understand it :?? How are associative arrays elements sorted?
Randomly? First-added is first?

> (Incidentally, I didn't test this, but I'll bet that "seeding" a local
> RANDOM like that ruins the repeatable sequence of the global RANDOM.)

    Probably, but I don't care.

    Thanks a lot for your help :))) Since speed is not a problem,
I'll try with the first solution, although mine is fast enough for my
needs (and in a month I probably still remember how it works ;)).
Thanks again.

    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