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

Re: shuffle array



On Fri, Nov 29, 2019 at 09:50:12AM +0100, Emanuel Berg wrote:
> $ local -a arg_array
> $ arg_array=(1 2 3 a b c)
> $ print -l $arg_array | shuf | read -A new_array
> $ echo $new_array
> 1
> 
> doesn't work, just one element...

`read` only reads one line.

I don't remember where this came from, but I've been using

shuffle() {
  declare -A h
  local +h -Z 5 RANDOM=$EPOCHSECONDS
  integer i
  for ((i=1; i <= $#; ++i)) { h[$i.$RANDOM]=$argv[i] }
  reply=( $h )
}



Messages sorted by: Reverse Date, Date, Thread, Author