#!/usr/cs/bin/perl # # Randomly extracts text from a file using a specified delimiter # # Example usage: creation of Zen poems # http://www.cs.virginia.edu/~an7s/zen/ # ($#ARGV >= 0) || die "Usage: $0 [-norepeat] [-n ] [-d ]\n" . "\t : name of a file\n" . "\t -norepeat : disallows repetition of output\n" . "\t -n : number of paragraphs, default 1\n" . "\t -d : regular expression, default '^\$'\n" . "\t\t\te.g. '##' or '[0-9]*'\n" . "\t\t\ta '/' must be sent as '\\/'\n" . "\t\t\ta carriage return must be sent as '\\n'\n" . "\t\t\ta tab must be sent as '\\t'\n" . "\t\t\tan empty line must be sent as '^\$'\n"; # Set defaults $norepeat = 0; $number = 1; $delimiter = '^$'; # Parse arguments $filename = shift(@ARGV); open(LINES, $filename) || die "Couldn't open $filename\n"; while (@ARGV) { if ($ARGV[0] eq "-n") { shift(@ARGV); $number = shift(@ARGV); ($number =~ /^[0-9]+$/) || die "$number is not a number\n"; } elsif ($ARGV[0] eq "-d") { shift(@ARGV); $delimiter = shift(@ARGV); } elsif ($ARGV[0] eq "-norepeat") { shift(@ARGV); $norepeat = 1; } else { die "$ARGV[0] is not a valid argument\n"; } } # Read in the fields srand; $i = 0; if ($delimiter ne "^\$") { while ($line = ) { chop($line) if ($delimiter ne "\\n"); foreach (split($delimiter, $line)) { $paras[$i++] .= $_; } } } else { while ($line = ) { ($line =~ /^$/) ? $i++ : ($paras[$i] .= $line); } } close(LINES); # Select the randomly-generated fields for printing if ($norepeat) { ($number <= $#paras + 1) || die "Must repeat for generating $number items!\n"; for (@list = (), $i = 0 ; $i < $number ;) { $j = int(rand($#paras+1)); $found = 0; foreach $k (@list) { $found = 1 && break if ($j == $k); } push(@list, $j) && $i++ if (!$found); } foreach $k (@list) { print "$paras[$k]\n"; } } else { for ($i = 0 ; $i < $number ; $i++) { print "$paras[rand($#paras+1)]\n"; } }