Since it seemed like a nice exercise to work on I played with this some myself. Goals being to try to avoid global variables, use subroutines and keep MAIN 'uncluttered' and pass arguments to subs as needed.
I think I did okay (holding breath), but I'm wondering about things like: my @sorted_keys = sort_results(\%score, \%opt); ## [...] sub sort_results { my %scores = %{ (shift) }; my %opt = %{ (shift) }; ## [...] } In the subroutine I'm dereferencing the hash and array and placing the values into new variables, correct? Should I instead be working directly on the original values (via a reference)? Would that be something like: sub sort_results { my ($score_ref, $opt_ref) = @_; ## etc... } (Thanks for your always helpful advice and comments!) -K #!/usr/bin/perl use warnings; use strict; # from perl.beg (Stuart White) # desired result: # (attempted shots) # # Marbury 1 # Jackson 3 # Hardaway 1 # Stoudemire 2 # Bowen 3 # Duncan 2 # Williams 1 # Marion 2 # Ginobili 1 { use Getopt::Std; my (%opt, $total_attempts); getopts('hl', \%opt); # commandline options my %score = get_data(); my @sorted_keys = sort_results(\%score, \%opt); print (format_output([EMAIL PROTECTED], \%score)); } ## END MAIN ## ## BEGIN SUBS ## sub get_data { my %scores; while (<DATA>) { if (/.*\] (\w+).*/) { # get player's name $scores{$1}++; } } return %scores; } sub sort_results { my %scores = %{ (shift) }; my %opt = %{ (shift) }; unless (%opt) { # default is sort by name my @sorted_keys = sort keys %scores ; # sorted by key } else { my $sort_hi if ($opt{h}); my @keys_sorted_by_value = sort { compare ($scores{$a}, $scores{$b}, $opt{h}) } keys %scores; # sorted by value } } sub compare { # sorts high & low scores my ($aa, $bb, $sort_hi) = @_; ($aa, $bb) = ($bb, $aa) if $sort_hi; # reverse if sort high (option h) return $aa <=> $bb; } sub format_output { my @sorted_keys = @{ (shift) }; my %score = %{ (shift) }; my $total_attempts; my $output = "\n"; foreach (@sorted_keys) { $output .= sprintf "%12s %2s\n", $_, $score{$_}; $total_attempts += $score{$_}; } $output .= "================\n"; $output .= "TOTALS: $total_attempts\n\n"; } __DATA__ (11:23) [SAN 2-0] Bowen Jump Shot: Made (2 PTS) (11:10) [PHX] Marbury Jump Shot: Missed (11:07) [PHX] Marion Jump Shot: Missed (10:51) [SAN 4-0] Jackson Jump Shot: Made (2 PTS) (10:23) [SAN] Jackson Jump Shot: Missed (9:43) [SAN] Duncan Jump Shot: Missed (8:43) [SAN] Bowen Jump Shot: Missed (8:31) [PHX] Stoudemire Jump Shot: Missed (8:09) [SAN] Jackson Jump Shot: Missed (7:42) [PHX] Hardaway Jump Shot: Missed (7:09) [PHX] Stoudemire Jump Shot: Missed (6:50) [SAN] Bowen Jump Shot: Missed (6:15) [SAN 10-2] Duncan Jump Shot: Made (4 PTS) (5:52) [PHX] Williams Jump Shot: Missed (5:46) [PHX 5-10] Marion Jump Shot: Made (5 PTS) (5:06) [SAN 12-7] Ginobili Jump Shot: Made (2 PTS) ## -- Kevin Pfeiffer International University Bremen -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]