Linux.com

Shuffle

Posted by: Anonymous Coward on March 23, 2006 06:31 AM
Two of my favorite command-line constructs are:

Pick a random file (e.g. MP3)


    ls | perl -e '@a=<>;print $a[rand(@a)]'

and:

Shuffle the contents of a file


    cat foo | perl -e '@a=<>;while(@a){print splice(@a,rand(@a),1)}'

The latter is awash with Perl Wizardry (using an array in scalar context, the splice() function, etc.) but it's simplistic in its brevity.

#

Return to Command line Perl for sysadmins