Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

RE: Post Election Day Perl

by chromatic (Archbishop)
on Nov 09, 2000 at 03:25 UTC ( #40651=note: print w/replies, xml ) Need Help??


in reply to Post Election Day Perl

This could be made shorter by removing whitespace, but as the interpreter doesn't care about it, I don't think it should count.

Also, I focused only on the Florida count, as that's what I thought Fearless Perl Commando meant. (Yes, that's his title at EDC.)

#!/usr/bin/perl -w use LWP::Simple; my ($c, %d, @o) = get("http://www.cnn.com/ELECTION/2000/results/FL/ind +ex.html"); for ('Bush', 'Gore') { if ($c =~ m!$_</a>.+?dPercent">([0-9,]+)!s) { ($d{$_} = $1) =~ tr/,//d; } } @o = sort { $d{$a} cmp $d{$b} } keys %d; my $d = "$o[1] leads $o[0] by @{[($d{$o[1]} - $d{$o[0]})]} votes!"; $d = "It's a tie!" if $d{$o[1]} == $d{$o[0]}; die unless open(I, "vote.log"); @o = <I>; $_ = $o[-1]; exit if (/$d$/); select O if (open(O, ">>vote.log")); print (localtime() . "\n$d\n"); exit if !(open(A, "addresses")); my @a = <A>; die "No sendmail!" unless open(S, "|/usr/lib/sendmail -t"); print S <<E; Subject: Vote results as of @{[ ''. localtime() ]} Bcc: @{[@a]} $d E

Update: Hmm, what's the proper balance between error checking, graceful degradation when CNN changes the page format, and Doing the Right Thing with as few errors as possible? This would make a good Discussion.

/msg me your thoughts.

Replies are listed 'Best First'.
RE: RE: Post Election Day Perl
by chromatic (Archbishop) on Nov 09, 2000 at 22:55 UTC
    How gauche, to respond to my own post. amelinda had the right idea with using lynx instead of LWP::Simple. Here's one that has whitespace and still weighs in at 557 bytes, performs the logging and mailing (but only if something has changed):
    #!/usr/bin/perl $_ = `lynx -dump http://www.cnn.com/ELECTION/2000/results/FL/index.htm +l`; push @c, (/(Bush).{4}([0-9,]{9})/s), (/(Gore).{4}([0-9,]{9})/s); tr/,//d for @c[1,3]; $d = "$c[0] leads $c[2] by @{[$c[1] - $c[3]]} votes!"; $d = "It's a tie!" if $c[1] == $c[3]; die if !open(I, "vote.log"); chomp($_ = $i) while ($i = <I>); exit if (/$d$/); die if !(open(O, ">>vote.log")); $i = localtime(); print O ("$i\n$d\n"); die if !open(A, "addresses"); die if !open(S, "|/usr/lib/sendmail -t"); print S <<E; Subject: Vote results as of $i Bcc: @{[<A>]} $d E
    Removing whitespace would take me down at least to 531. Of that, 29 characters are in the messages (someone leads, someone tied), 26 characters are used in filenames (I could get that down to 3), and the URL is 54 characters. Using +shift or @ARGV and the other optimizations could take me close to 400 bytes.

    ph3@r!

    Update:

    Here's a version with some optimizations that occurred to me in the shower. (See, 'Where Do You Get Your Best Ideas?') It's a slim 516 bytes, without removing whitespace (30 characters or more), trimming filenames, removing error checking (27 characters), but it corrects a subtle flaw in the original.

    #!/usr/bin/perl $_ = `lynx -dump http://www.cnn.com/ELECTION/2000/results/FL/index.htm +l`; push @c, (/(Bush|Gore).{4}([0-9,]{9})/sg); y/,//d for @c[1,3]; $d = "$c[0] leads $c[2] by @{[$c[1] - $c[3]]} votes!"; $d = "It's a tie!" if $c[1] == $c[3]; die if !open(I, "vote.log"); ($_ = $i) while ($i = <I>); exit if /$d/; die if !(open(O, ">>vote.log")); $i = localtime(); print O ("$i\n$d\n"); die if !open(A, "addresses") || !open(S, "|/usr/lib/sendmail -t"); print S "Subject: Vote results as of $i Bcc: @{[<A>]} $d";
RE: RE: Post Election Day Perl
by vroom (Pope) on Nov 09, 2000 at 03:41 UTC
    Yeah the florida vote is what I'm interested in. I was grabbing it from the front page but this works too.

    vroom | Tim Vroom | vroom@cs.hope.edu

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://40651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2020-01-23 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?