#!/usr/local/bin/perl -w # Program: filmstat.pl # Author: Spencer Shimko # Purpose: Display top films based on user input # Usage: ./filmstat.pl [year] [numoffilms] # Created: 3/30/04 # Modified: 4/01/04 ############################ # sub parse cmd line # ############################ sub getcmdline{ # parse command line and assign to some vars while ( defined $ARGV[0] ){ $arg = shift @ARGV; if ( $arg =~ /^\d{1,3}$/ ){ $num2show = $arg unless defined $num2show; } elsif ($arg =~ /^\d{4}$/ ){ $year2show = $arg unless defined $year2show; } else { $filename = $arg unless defined $filename; } } # if we don't have a filename die if ( ! defined $filename ){ die "$0: require a filename"; } } ############################# # end sub parse cmd line # ############################# ############################# # start sub by_score_votes # ############################# sub by_score_votes{ # return higher hash by score then votes if ( $$a{'score'} > $$b{'score'} ){ return -1; } elsif ( $$a{'score'} < $$b{'score'} ){ return 1; } else { # if we reach this point scores are equal so sort on # votes return ( $$b{'votes'} <=> $$a{'votes'} ); } } ############################# # end sub by_score_votes # ############################# ############################# # start sub display_results # ############################# sub display_results{ print "\nTop "; # if max num requested show if ( defined $num2show ){ # make sure requested number is smaller then total if ( $num2show > ( $#sorted + 1) ){ $num2show = $#sorted + 1; } print "$num2show "; } print "Films "; # if year requested if ( defined $year2show ){ print "from Year $year2show "; # this is to correct the num2show for the year requested $yrcnt = 0; foreach (@sorted){ if ( $$_{'year'} == $year2show ){ $yrcnt++; } } if ( defined $num2show ){ if ( $num2show > $yrcnt ){ $num2show = $yrcnt; } } else { $num2show = $yrcnt; } } print "(source: $filename)\n\n"; # i need an index for my for loop control $num2show = $#sorted + 1 unless defined $num2show; for ( $index=0, $cnt=0; $cnt < $num2show; $index++,$cnt++ ){ if ( defined $year2show ){ if ( $sorted[$index]{'year'} == $year2show ){ print "$sorted[$index]{'score'}: $sorted[$index]{'name'} ($sorted[$index]{'year'}) [$sorted[$index]{'votes'} votes]\n"; } else{ $cnt--; } } else { print "$sorted[$index]{'score'}: $sorted[$index]{'name'} ($sorted[$index]{'year'}) [$sorted[$index]{'votes'} votes]\n"; } } } ############################# # end sub display_results # ############################# ############################# # start main # ############################# getcmdline(); # read/parse cmd line # open file open (FILE, $filename) or die "$0: cannot open $filename, $!"; while ( ){ chomp; # remove blanklines and comments s/^\s*$//; s/\s*\#.*$//; s/^(\s+)(.*)/$2/; s/(.*)(\s+)$/$1/; if ( /^$/ ){ next; } if ( /^\d*\.\s+(\d+\.\d+)\/.*?\s+(.*)\s+\((\d+)\)\s+(\d{4})$/ ){ $score = $1; $name = $2; $votes = $3; $year = $4; } push @list, ({'name' => $name, 'score' => $score, 'votes' => $votes, 'year' => $year}); } close (FILE); # sort list of movies @sorted = sort by_score_votes @list; display_results();