18

So I have a file. Let’s say it looks like this (it's actually longer):

1234
2134
3124
4123

What is the best way to shuffle the lines in that file?

6
  • what do u mean by randomly looping? Commented Nov 16, 2012 at 12:13
  • 2
    I think he means shuffle, right Bill ? Commented Nov 16, 2012 at 12:16
  • @sputnick Yes, That's what i mean Commented Nov 16, 2012 at 12:24
  • 3
    The Perl FAQ covers this, as well as many other common Perl questions: How do I shuffle an array randomly? Commented Nov 16, 2012 at 18:00
  • Besides the FAQ which @Andy kindly points out, there are 40 Perl questions that mention “shuffle” that you should probably study. Commented Dec 6, 2012 at 1:29

1 Answer 1

34
#!/usr/bin/env perl

use strict;
use warnings;

use List::Util qw/shuffle/;

my @arr = shuffle <>;

print @arr;

Usage :

./script file.txt
Sign up to request clarification or add additional context in comments.

1 Comment

List::Util is a core module, if that's important to you. (It is to me. I am a module snob.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.