use strict; use warnings; use YAML 'Dump'; use LWP::Simple 'get'; use Text::Table (); use Memoize 'memoize'; use Hash::Util 'lock_hash'; use List::Util 'first'; use vars qw( @Fields %Races ); @Fields = ( map { s/^\s+//; s/\s+$//; s/\s+\W*(\w)/uc $1/eg; s/\W+(\w)/uc $1/ge; $_; } split /[\r\n]+/, q[ State County ID Precinct Number Office ID Office Name District Candidate ID Candidate Name Suffix Incumbent Code Party ID Precincts Reporting Total Precincts Votes for Candidate PercentVote Total Vote] ); %Races = ( Presidential => 'PresSW', StateLegislative => 'StHouseDst', USHouse => 'USHouseDst', County => 'counties', Judicial => 'judicialdst' ); while ( my ( $sub, $file ) = each %Races ) { $sub .= "Results"; { no strict 'refs'; *$sub = sub { FetchResults( "http://electionresults.sos.state.mn.us/20041102/m +edia/$file.txt" ); }; } memoize $sub; } $| = 1; main(); exit; sub RacesWithGreenPartyCandidates { my @r = @_; my %green_races; @green_races{ map $_->{'OfficeName'}, grep $_->{'PartyID'} =~ /GP|BL/, @r } = (); return grep exists $green_races{$_->{'OfficeName'}}, @r; } sub LaJuneThomasLange { my @r = @_; my $race = first { $_->{'CandidateName'} eq 'LAJUNE THOMAS LANGE' } @r; $race = $race->{'OfficeName'}; return grep $_->{'OfficeName'} eq $race, @r; } sub main { for ( \ &PresidentialResults, \ &USHouseResults, \ &StateLegislativeResults ) { my @results = $_->(); Report( \ &RacesWithGreenPartyCandidates, \ @results ); } Report( \ &LaJuneThomasLange, [ JudicialResults() ] ); 1; } sub Report { my $filter = shift; my @all_results = $filter->( @{ shift() } ); my %races; $races{$_->{'OfficeName'}} = () for @all_results; for my $race ( sort keys %races ) { my @results = grep $_->{'OfficeName'} eq $race, @all_results; my $tbl = Text::Table->new( 'Party', 'Candidate', 'Percent' ); $tbl->load( map { [ $_->{'PartyID'}, $_->{'CandidateName'}, sprintf( "%0.1f", $_->{'PercentVote'} ) ] } sort { $b->{'PercentVote'} <=> $a->{'PercentVote'} } @results ); print( "$race\n" . sprintf( "%0.1f", 100 * ( $results[0]{'PrecinctsReporting'} / + $results[0]{'TotalPrecincts'} ) ) . " precincts reporting\n" . $tbl . "\n" ); } } sub FetchResults { my $url = shift; return map { my %h; @h{@Fields} = split /;/; for ( values %h ) { s/^\s+//; s/\s+/ /g; s/\s+$//; } lock_hash( %h ); \ %h } split /[\r\n]+/, get( $url ); }

In reply to Minnesota election results watcher by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.