#!/usr/athena/bin/perl
#
# PROGRAM: 	rollingpickline.pl
# AUTHOR:  	Erik Nygren <nygren@mit.edu>
# DATE:		2000.04.06
# USAGE:	rollingpickline file [files...]
# PURPOSE:	Pick a line from one of the files
#		or from stdio if none specified.
#	        Stores a /tmp/.$ENV{USER}.rpllast file which contains 
#               a monitonically increasing value.			      
# CAVEATS:	Blank lines in input file may be picked
#

$lastfile = "/tmp/.$ENV{USER}.rpllast";
@lines = <>;	# Load the files specified on the command line into @lines
$pos=0;
if (open LAST, "<$lastfile") {
    $pos=<LAST>;
    close LAST;
    $pos++;
} else {
    # on failure, just pick one randomly.
    $pos = int(rand(scalar(@lines)));
}
if (open LAST, ">$lastfile") { # don't whine on failure
    print LAST $pos;
    close LAST;
}
print $lines[$pos % scalar(@lines)];	# Pick a line and print it

