zsh-workers
 help / color / mirror / code / Atom feed
* Would this (o) be very difficult to add?
@ 2003-09-23 16:12 DervishD
  2003-09-23 16:55 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: DervishD @ 2003-09-23 16:12 UTC (permalink / raw)
  To: Zsh

    Hi all :)

    Sometimes we want to do globbing and sort the output randomly:
when generating playlists, when generating image slideshows, etc...
So I've thought that it would be good if we can do:

    print -l *(or)
    print -l *(Or)

    Please note that since the sort criteria is 'random', (o) and (O)
have exactly the same effect...

    If you use qsort or something like that, a quite simple
pseudo-random (we want apparent chaos, not cryptographical quality)
order can be achieved using a comparison function that generates a
random integer and returns, for example, 'first is greater' if the
random integer is even, 'second is greater' if the random integer is
odd and 'equal' if the random integer is 0. In fact, if you use the
standard qsort behaviour, you can return the random integer minus the
randomize range, so it will be <0, 0 or >0 appropriately.

    If this is not much bloat, I would love having such a glob
qualifier :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-23 16:12 Would this (o) be very difficult to add? DervishD
@ 2003-09-23 16:55 ` Bart Schaefer
  2003-09-24 14:01   ` DervishD
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2003-09-23 16:55 UTC (permalink / raw)
  To: Zsh

On Sep 23,  6:12pm, DervishD wrote:
}
}     Sometimes we want to do globbing and sort the output randomly:
} when generating playlists, when generating image slideshows, etc...

Try this:

	print -lP *(e:'REPLY=%0(l..$RANDOM)"$REPLY"':)


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-23 16:55 ` Bart Schaefer
@ 2003-09-24 14:01   ` DervishD
  2003-09-27 11:18     ` DervishD
  0 siblings, 1 reply; 8+ messages in thread
From: DervishD @ 2003-09-24 14:01 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }     Sometimes we want to do globbing and sort the output randomly:
> } when generating playlists, when generating image slideshows, etc...
> Try this:
> 	print -lP *(e:'REPLY=%0(l..$RANDOM)"$REPLY"':)

    Very clever, I'd never have thought about something like that...
I tried something like this, inserting $RANDOM before each filename
in a for loop, redirecting to sort (or using the (o) modifier to sort
the list by name) and using cut (or ${...##...}) for getting rid of
the random number. No doubt your example is better. Far better ;))

    Anyway I keep thinking that the (or) modifier would be useful ;)

    Thanks a lot for your answer. It's a neat, short and fast way of
generating random lists :)))

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-24 14:01   ` DervishD
@ 2003-09-27 11:18     ` DervishD
  2003-09-27 20:22       ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: DervishD @ 2003-09-27 11:18 UTC (permalink / raw)
  To: Bart Schaefer, Zsh

    Hi Bart and zsh workers :))

 * DervishD <raul@pleyades.net> dixit:
>  * Bart Schaefer <schaefer@brasslantern.com> dixit:
> > }     Sometimes we want to do globbing and sort the output randomly:
> > } when generating playlists, when generating image slideshows, etc...
> > Try this:
> > 	print -lP *(e:'REPLY=%0(l..$RANDOM)"$REPLY"':)
>     Very clever, I'd never have thought about something like that...

    This solution has worked great for generating, for example, a
file containing the names of the files in random order. The problem
is that I cannot use this in a command: command $(print -lP...)
results in 'command file name 1 file name 2', unquoted :((( Strictly
speaking, characters like '[' are quoted, but spaces arent, and the
names which I want to order randomly usually contain spaces.

    I've tried something like 'array=($(print -lP...))', but since
the expansion has spaces unquoted, the elements of the array end up
being 'file', 'name', '2', 'file', 'name', '1', and so on.

    How can I get the result of the 'e' glob qualifier quoted, so I
can use command $(print -lP...) and get a working command?

    Thanks a lot :))

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-27 11:18     ` DervishD
@ 2003-09-27 20:22       ` Bart Schaefer
  2003-09-28  9:25         ` DervishD
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2003-09-27 20:22 UTC (permalink / raw)
  To: Zsh

On Sep 27,  1:18pm, DervishD wrote:
} Subject: Re: Would this (o) be very difficult to add?
}
} [...] I cannot use this in a command: command $(print -lP...)
} results in 'command file name 1 file name 2', unquoted :(((

Since you're using "print -l" you have the filenames one per line.
Therefore, just quote the entire thing and split the output on newlines,
and all should be well:

    command ${(f)"$(print -lP ...)"}

Zsh is even clever enough to deal properly with nested double quotes
when ${...} or $(...) are involved.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-27 20:22       ` Bart Schaefer
@ 2003-09-28  9:25         ` DervishD
  2003-09-28 17:46           ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: DervishD @ 2003-09-28  9:25 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

    Nice to see you back ;)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } [...] I cannot use this in a command: command $(print -lP...)
> } results in 'command file name 1 file name 2', unquoted :(((
> Since you're using "print -l" you have the filenames one per line.
> Therefore, just quote the entire thing and split the output on newlines,
> and all should be well:
>     command ${(f)"$(print -lP ...)"}

    Oh, I'm really *stupid*. I was trying the same but with the (j)
flag... I was using it to fold instead of the correct (f), and all I
got was an 'error in flags' message :((((

    Thanks, this solve entirely my problem :)) 

    Another thing I'm doing with all this is storing the generated
list on an array, because sometimes I wan't to have the list passed to
a command and stored in a file, and I do with the array:

    array=(${(f)...})
    command $array
    print -l $array > file.list

    Can this be done directly in the command line for the 'command',
or I'd better stuck with the array and the redirection? In fact for
me is very convenient to have the list on an array, for further
manipulation, for example, it's just curiosity, not need ;)

> Zsh is even clever enough to deal properly with nested double quotes
> when ${...} or $(...) are involved.

    Good boy, Zsh, good boy ;)) Thanks a lot for your answer :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-28  9:25         ` DervishD
@ 2003-09-28 17:46           ` Bart Schaefer
  2003-09-28 18:48             ` DervishD
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2003-09-28 17:46 UTC (permalink / raw)
  To: Zsh

On Sep 28, 11:25am, DervishD wrote:
}
}     Another thing I'm doing with all this is storing the generated
} list on an array, because sometimes I wan't to have the list passed to
} a command and stored in a file, and I do with the array:
} 
}     array=(${(f)...})

If you're storing in an array, you should skip the $(print -lP ...) part
(which requires forking a subshell) and do this instead:

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

}     command $array
}     print -l $array > file.list
} 
}     Can this be done directly in the command line for the 'command',
} or I'd better stuck with the array and the redirection?

I'm not entirely sure, but I suspect you mean something like this:

    command ${(f)"$(print -lP ... >&1 > file.list)"}

This only works if you have the MULTIOS option set; if you don't use that,
you need something like

    command ${(f)"$(print -lP ... | tee file.list)"}


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Would this (o) be very difficult to add?
  2003-09-28 17:46           ` Bart Schaefer
@ 2003-09-28 18:48             ` DervishD
  0 siblings, 0 replies; 8+ messages in thread
From: DervishD @ 2003-09-28 18:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }     array=(${(f)...})
> If you're storing in an array, you should skip the $(print -lP ...) part
> (which requires forking a subshell) and do this instead:
>     array=( *(e:'REPLY="%0(l..$RANDOM)$REPLY"':) )
>     array=( ${(%)array} )

    Oh, I forgot the (%) flag and I stuck with the '-P' for
interpreting %0. BTW, how works exactly the %0(l..$RANDOM)
construction? I know that the (l..) is for padding with a random
number, and I suppose the %0 is there for avoiding printing it, but I
don't know how it exactly works... If you're tired of this feel free
to ignore me ;) For me, %0 can only be a job specification, because I
don't know about any prompt escape that fits just %NUMBER :?

> }     Can this be done directly in the command line for the 'command',
> } or I'd better stuck with the array and the redirection?
> I'm not entirely sure, but I suspect you mean something like this:
>     command ${(f)"$(print -lP ... >&1 > file.list)"}

    Yes, it will do. I just didn't know where should I do the
redirection (or if it was possible at all...). Thanks a lot for
showing :))

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-09-28 18:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-23 16:12 Would this (o) be very difficult to add? DervishD
2003-09-23 16:55 ` Bart Schaefer
2003-09-24 14:01   ` DervishD
2003-09-27 11:18     ` DervishD
2003-09-27 20:22       ` Bart Schaefer
2003-09-28  9:25         ` DervishD
2003-09-28 17:46           ` Bart Schaefer
2003-09-28 18:48             ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).