shuffle_list.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env perl
  2. # Copyright 2013 Johns Hopkins University (author: Daniel Povey)
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  10. # KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  11. # WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. # MERCHANTABLITY OR NON-INFRINGEMENT.
  13. # See the Apache 2 License for the specific language governing permissions and
  14. # limitations under the License.
  15. if ($ARGV[0] eq "--srand") {
  16. $n = $ARGV[1];
  17. $n =~ m/\d+/ || die "Bad argument to --srand option: \"$n\"";
  18. srand($ARGV[1]);
  19. shift;
  20. shift;
  21. } else {
  22. srand(0); # Gives inconsistent behavior if we don't seed.
  23. }
  24. if (@ARGV > 1 || $ARGV[0] =~ m/^-.+/) { # >1 args, or an option we
  25. # don't understand.
  26. print "Usage: shuffle_list.pl [--srand N] [input file] > output\n";
  27. print "randomizes the order of lines of input.\n";
  28. exit(1);
  29. }
  30. @lines;
  31. while (<>) {
  32. push @lines, [ (rand(), $_)] ;
  33. }
  34. @lines = sort { $a->[0] cmp $b->[0] } @lines;
  35. foreach $l (@lines) {
  36. print $l->[1];
  37. }