Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

RE: RE: Post Election Day Perl

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


in reply to RE: Post Election Day Perl
in thread Post Election Day Perl

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";

Log In?
Username:
Password:

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

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