use strict; use warnings; my @valid_candidates = ( 'Alice', 'Bob', 'Charlotte', 'David', 'Eve' ); my %tally_for = map { lc($_) => [0] } @valid_candidates; print "Welcome to the Die Bold 691549 Electrified Election Estimator\n"; print "Your candidates are:\n"; print map { "$_\n" } sort @valid_candidates; print "Enter one name per line, end with EOF.\n"; while ( defined( my $candid_date = <> ) ) { chomp $candid_date; my $candidate; foreach ( @valid_candidates ) { last if lc $candid_date eq ( $candidate = lc $_ ); } if ( exists $tally_for{ $candidate } ) { $tally_for{ $candidate }->[0]++; } else { warn "Write-in for $candid_date\n"; $tally_for{ lc $candid_date }->[0]++; } } my @winner_first = reverse sort { $tally_for{$a}->[0] <=> $tally_for{$b}->[0] } keys %tally_for; foreach my $who ( @winner_first ) { printf "%10s : %d\n", $who, $tally_for{$who}->[0]; }