Subject: Re: make(1) variables: sort and disorder [patch]
To: Alan Barrett <apb@cequrux.com>
From: Mike M. Volokhov <mishka@apk.od.ua>
List: tech-userlevel
Date: 06/01/2005 15:55:28
On Wed, 1 Jun 2005 12:33:42 +0200
Alan Barrett <apb@cequrux.com> wrote:
[snip]
> > .It Cm \&:O
> > Order every word in variable alphabetically.
> > +.It Cm \&:Or
> > +Reverse words in variable from head to tail.
>
> It's not clear whether "head to tail" refers to the original order
> or alphabetical order. So I'd say something like "Order words in
> variable in reverse alphabetical order." Possibly also mention that
> ${variable:Or} is equivalent to ${variable:O:[-1..1]}.
This is a not. It really just reverts word list from head to tail as it
':[-1..1]' did :-/
Unfortunately, I haven't found this feature before and now I doesn't
see any reason to left it in code, even as shorten ':O:[-1..1]'. Did
you?
> > +.It Cm \&:Ox
> > +Randomize words in variable. The results will be different each
> > +time you are referring to the modified variable; use the assignment
> > +with expansion
> > +.Pq Ql Cm \&:=
> > +to prevent such behaviour.
>
> Perhaps this explanation would benefit from an example?
Added to man page.
> > If randomization will be done on unsorted
> > +source sequence, it may produce an alphabetically ordered result;
> > +to avoid this explicitly sort the source, and then randomize it as
> > +.Ql Cm \&:O:Ox .
>
> It should be expected that shuffling the words will ocasionally produce
> ordered output. That's part of the nature of random shuffling. There
> should be no need to try to prevent it. Trying to prevent ordered otput
> from a random process actually reduces the randomness of the process.
> So I would remove this sentence frmo the man page.
OK. Done.
> > * Input:
> > * str String whose words should be sorted
> > + * otype How to order: (s)ort, (r)everse, intermi(x)
>
> The term "intermix" doesn't tell me that it means "random". I suggest saying
> "(x) random".
Fixed.
> > + /* intermixed variable should return different values each time */
> > + gettimeofday(&rightnow, NULL);
> > + srandom(rightnow.tv_sec + rightnow.tv_usec);
>
> I don't think there's any gain from calling srandom() more than once per
> program execution.
Improved, thanks.
> > + for (i = 0; i < ac; i++) {
> > + ai[i].rnd = random();
> > + ai[i].avi = av[i];
> > + }
>
> I suggest using the shuffling method used in the get_shuffle() function
> in src/usr.bin/shuttle/shuffle.c. It has some good properties proved by
> Knuth.
Already done after some private discussion with Greg Troxel. I've used
just the same variant as in shuffle, but without seen it before :-)
--
Mishka.