From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4511 invoked from network); 24 Jul 2005 07:36:32 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 24 Jul 2005 07:36:32 -0000 Received: (qmail 59471 invoked from network); 24 Jul 2005 07:36:25 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 24 Jul 2005 07:36:25 -0000 Received: (qmail 18573 invoked by alias); 24 Jul 2005 07:36:19 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9140 Received: (qmail 18563 invoked from network); 24 Jul 2005 07:36:18 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 24 Jul 2005 07:36:18 -0000 Received: (qmail 58458 invoked from network); 24 Jul 2005 07:36:18 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 24 Jul 2005 07:36:12 -0000 Received: from 212.red-80-35-44.pooles.rima-tde.net ([80.35.44.212] helo=localhost) by ns9.hostinglmi.net with esmtpa (Exim 4.51) id 1Dwb1w-0008Ig-Ho; Sun, 24 Jul 2005 09:36:13 +0200 Date: Sun, 24 Jul 2005 09:39:14 +0200 From: DervishD To: Bart Schaefer Cc: Zsh Users Subject: Re: Sorting file names randomly Message-ID: <20050724073914.GA2665@DervishD> Mail-Followup-To: Bart Schaefer , Zsh Users References: <20050723194240.GA32416@DervishD> <20050723212657.GA744@DervishD> <1050724064415.ZM20425@candle.brasslantern.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1050724064415.ZM20425@candle.brasslantern.com> User-Agent: Mutt/1.4.2.1i Organization: DervishD X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - ns9.hostinglmi.net X-AntiAbuse: Original Domain - sunsite.dk X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - dervishd.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 Hi Bart :) * Bart Schaefer 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...