chiark / gitweb /
New features:
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 Sep 2013 20:40:23 +0000 (21:40 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 Sep 2013 20:40:23 +0000 (21:40 +0100)
* random-word: -F<n> option for using only first <n> lines of source file.

debian/changelog
scripts/random-word

index 0fa99b32e20fc0a1dcdf67bd477ff826732e57a3..e0dfcd2c497582181a8d2fdb9b9ae2238175cf3d 100644 (file)
@@ -1,5 +1,8 @@
 chiark-utils (4.2.1~~iwj1) unstable; urgency=low
 
+  New features:
+  * random-word: -F<n> option for using only first <n> lines of source file.
+
   Code cleanups:
   * random-word: Some perl-mode emacs formatting glitch workarounds.
   * random-word: use strict; -w.
index 427e4d08795710586fb6f02229f78cadb98b0ac4..649af152a3c074a55be6e3b3ef33e081ec03881f 100755 (executable)
@@ -25,6 +25,7 @@ use POSIX;
 our $want= 1;
 our $filename= "/usr/share/dict/words";
 our @randfile= ("/dev/urandom", "/dev/random");
+our $filemaxlen;
 
 sub fail ($) { die "random-word: $_[0]\n"; }
 open D, ">/dev/null" or fail("open /dev/null: $!");
@@ -37,6 +38,8 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) {
        $want= $1;
     } elsif (m/^\-f/ && length > 2) {
        $filename= $'; #';
+    } elsif (m/^\-F(\d+)$/) {
+       $filemaxlen= $1;
     } elsif (m/^\-r/ && length > 2) {
        @randfile= ($'); #');
     } elsif (m/^\-D$/) {
@@ -66,7 +69,16 @@ $r->autoflush(0);
 our $w= new IO::File $filename, 'r';
 $w or fail("cannot open $filename: $!");
 debug("open $filename ok");
-our @words= <$w>;
+our @words;
+if (defined $filemaxlen) {
+    while (@words < $filemaxlen) {
+       my $l = <$w>;
+       last unless defined $l;
+       push @words, $l;
+    }
+} else {
+    @words= <$w>;
+}
 $w->error and fail("cannot read $filename: $!");
 debug("read $filename ok");