while () { $word = ; print if /$word/; } # Officiate is asked for input on every line of the file. # Officiate throttles monk; blacking out, you meditate on your error. #### while () { $line = $_; } my $word = ; print $line if $line =~ /$word/; # $line only contains the last line of the file! # Officiate makes bad decision based on wrong output. # Monk goes hungry. #### while () { push @lines, $_; } $word = ; foreach (@lines) { print if /$word/; } # File data is stored until $word is known. # Officiate gets correct output, is pleased while file is small. # Monk is thrashed when the disk thrashes on a large file. #### $word = ; while () { print if /$word/; } # There is no need to store the file data. # Harmony is achieved. #### #!/usr/bin/perl -W use warnings 'all'; use strict; # Scans the input file(s), breaking each line into fields. # Builds a key from prettified $type, and stores an array of # output lines in a hash under the key. Prints a list of all # the types seen in the file(s), and asks the user to choose one. # Prints the output lines stored for that key/type. my $limit = 30; my $usage = "Usage: $0 infile\n"; die $usage unless @ARGV; sub pretty { return ucfirst lc shift } my %lines; while (<>) { my ($whatever, $type, $num) = split; my $key = pretty($type); push @{ $lines{$key} }, "$type\t$num\n" if $num < $limit; } my @types = sort keys %lines; my $ask = "Please select a category:\n"; $ask .= "\t$_. $types[$_]\n" foreach 0 .. $#types; my $type; while (not $type) { print $ask; chomp(my $choice = ); $type = $types[$choice] or print "$choice was not an option!\n" and next; } my $key = pretty($type); print @{ $lines{$key} };