From 44d9110194da21397c5b41c7595daf67708a20a3 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 4 Sep 2013 21:40:23 +0100 Subject: [PATCH] New features: * random-word: -F option for using only first lines of source file. --- debian/changelog | 3 +++ scripts/random-word | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 0fa99b3..e0dfcd2 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,5 +1,8 @@ chiark-utils (4.2.1~~iwj1) unstable; urgency=low + New features: + * random-word: -F option for using only first lines of source file. + Code cleanups: * random-word: Some perl-mode emacs formatting glitch workarounds. * random-word: use strict; -w. diff --git a/scripts/random-word b/scripts/random-word index 427e4d0..649af15 100755 --- a/scripts/random-word +++ b/scripts/random-word @@ -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"); -- 2.30.2