Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Shuffle a subset of a list

Perl, 103 bytes Takes two lines of space-delimited numbers on stdin. First is the list, second is the indices. Prints shuffled result to stdout. perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+...

posted 6mo ago by jhnc‭  ·  edited 6mo ago by jhnc‭

Answer
#3: Post edited by user avatar jhnc‭ · 2026-03-22T11:32:13Z (6 months ago)
redundant final shuffle doesn't make code shorter
  • # Perl, 103 bytes
  • Takes two lines of space-delimited numbers on stdin.
  • First is the list, second is the indices.
  • Prints shuffled result to stdout.
  • ```
  • perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+/g;while($i>0){$j=int rand$i--;@a[@i[$i,$j]]=@a[@i[$j,$i]]}say"@a"'
  • ```
  • ```
  • perl -E'
  • @a = <>=~/\d+/g;
  • $i = @i = <>=~/\d+/g;
  • # Fisher-Yates/Durstenfeld shuffle
  • while ($i>0) {
  • $j = int rand $i--;
  • @a[ @i[$i,$j] ] = @a[ @i[$j,$i] ] # directly update @a
  • }
  • say "@a"
  • '
  • ```
  • [Try it online!][TIO-mn1o6rok]
  • [TIO-mn1o6rok]: https://tio.run/##TczNCoJAFIbhvVfxIQcsUtTKSmxk7sNmMeBYZxAJf4iIuvVpFi1avs/ivZuxL9wyGXRGz8toEE36GVVOanGuxSe9tJv0WhELyf/wuHFvVsR1tn6RFTzMGPXQEidJJXUjuSGOySolfmVjYqXefh5KHTqXIccWO@xR4IAjTiiRZwE8wzM8wzNQBl8 "Perl 5 – Try It Online"
  • # Perl, 103 bytes
  • Takes two lines of space-delimited numbers on stdin.
  • First is the list, second is the indices.
  • Prints shuffled result to stdout.
  • ```
  • perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+/g;while($i>1){$j=int rand$i--;@a[@i[$i,$j]]=@a[@i[$j,$i]]}say"@a"'
  • ```
  • ```
  • perl -E'
  • @a = <>=~/\d+/g;
  • $i = @i = <>=~/\d+/g;
  • # Fisher-Yates/Durstenfeld shuffle
  • while ($i>1) {
  • $j = int rand $i--;
  • @a[ @i[$i,$j] ] = @a[ @i[$j,$i] ] # directly update @a
  • }
  • say "@a"
  • '
  • ```
  • [Try it online!][TIO-mn1ofhvc]
  • [TIO-mn1ofhvc]: https://tio.run/##TczNCoJAFIbhvVfxIQcsUtTKSmxk7sNmMeBYZxAJf4iIuvVpFi1avs/ivZuxL9wyGXRGz8toEE36GVVOanGuxSe9tJv0WhELyf/wuHFvVsR1vn6RFTzMGPXQEidJJXUjuSGOySolfmVjYqXefh5KHTqXIccWO@xR4IAjTiiRZwE8wzM8wzNQBl8 "Perl 5 – Try It Online"
#2: Post edited by user avatar jhnc‭ · 2026-03-22T11:26:17Z (6 months ago)
oops, golfing decrement actually gave sattolo's algorithm
  • # Perl, 101 bytes
  • Takes two lines of space-delimited numbers on stdin.
  • First is the list, second is the indices.
  • Prints shuffled result to stdout.
  • ```
  • perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+/g;while($i--){$j=int rand$i;@a[@i[$i,$j]]=@a[@i[$j,$i]]}say"@a"'
  • ```
  • ```
  • perl -E'
  • @a = <>=~/\d+/g;
  • $i = @i = <>=~/\d+/g;
  • # Fisher-Yates/Durstenfeld shuffle
  • while ($i--) {
  • $j = int rand $i;
  • @a[ @i[$i,$j] ] = @a[ @i[$j,$i] ] # directly update @a
  • }
  • say "@a"
  • '
  • ```
  • [Try it online!][TIO-mn1nufqb]
  • [TIO-mn1nufqb]: https://tio.run/##TcxLCsIwFEbheVfxUy5UsaWtWrXESPZRMwg06g2lSB@IiG49ZuDA4fkG526HrvLzaHGxZpoHi2Q0z0R4ZeTxJD/5uV3lV0EsFf/D48adXRBn2fJFTnI/YTB9SyyUaRQ3xCk5reWvXEqs9TusY2Vi7wuUWGODLSrssMcBNcoiQmAERmAEBuroCw "Perl 5 – Try It Online"
  • # Perl, 103 bytes
  • Takes two lines of space-delimited numbers on stdin.
  • First is the list, second is the indices.
  • Prints shuffled result to stdout.
  • ```
  • perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+/g;while($i>0){$j=int rand$i--;@a[@i[$i,$j]]=@a[@i[$j,$i]]}say"@a"'
  • ```
  • ```
  • perl -E'
  • @a = <>=~/\d+/g;
  • $i = @i = <>=~/\d+/g;
  • # Fisher-Yates/Durstenfeld shuffle
  • while ($i>0) {
  • $j = int rand $i--;
  • @a[ @i[$i,$j] ] = @a[ @i[$j,$i] ] # directly update @a
  • }
  • say "@a"
  • '
  • ```
  • [Try it online!][TIO-mn1o6rok]
  • [TIO-mn1o6rok]: https://tio.run/##TczNCoJAFIbhvVfxIQcsUtTKSmxk7sNmMeBYZxAJf4iIuvVpFi1avs/ivZuxL9wyGXRGz8toEE36GVVOanGuxSe9tJv0WhELyf/wuHFvVsR1tn6RFTzMGPXQEidJJXUjuSGOySolfmVjYqXefh5KHTqXIccWO@xR4IAjTiiRZwE8wzM8wzNQBl8 "Perl 5 – Try It Online"
#1: Initial revision by user avatar jhnc‭ · 2026-03-22T11:19:34Z (6 months ago)
# Perl, 101 bytes

Takes two lines of space-delimited numbers on stdin.  
First is the list, second is the indices.  
Prints shuffled result to stdout.

```
perl -E'@a=<>=~/\d+/g;$i=@i=<>=~/\d+/g;while($i--){$j=int rand$i;@a[@i[$i,$j]]=@a[@i[$j,$i]]}say"@a"'
```

```
perl -E'
         @a = <>=~/\d+/g;
    $i = @i = <>=~/\d+/g;

    # Fisher-Yates/Durstenfeld shuffle
    while ($i--) {
        $j = int rand $i;
        @a[ @i[$i,$j] ] = @a[ @i[$j,$i] ] # directly update @a
    }

    say "@a"
'
```

[Try it online!][TIO-mn1nufqb]

[TIO-mn1nufqb]: https://tio.run/##TcxLCsIwFEbheVfxUy5UsaWtWrXESPZRMwg06g2lSB@IiG49ZuDA4fkG526HrvLzaHGxZpoHi2Q0z0R4ZeTxJD/5uV3lV0EsFf/D48adXRBn2fJFTnI/YTB9SyyUaRQ3xCk5reWvXEqs9TusY2Vi7wuUWGODLSrssMcBNcoiQmAERmAEBuroCw "Perl 5 – Try It Online"