« Making the Switch | Main | Athena, The Love Affair Begins »

With a Snip Snip Here and a Del Del There

My email has been giving me problems lately. It all started when I was trying out different operating systems that didn't really like each other. Moving the email files back and forth took it's toll on the little bastards, and I wound up with lots of copies of the same exact email sitting in my in box.

Well, then Perl to the rescue.

Note to anybody out there listening. It isn't PERL. It sure as hell isn't P.E.R.L. Perl is the language, perl is the program that compiles and interprets the language. Stop writing PERL.
Twenty lines of perl (could have been ten, but I wanted to make sure that I wouldn't loose anything important) and my inbox shrank from 200 megabytes, down to eight. 23,491 messages down to 1,238. That's a damn fine improvement. I'm quite happy with myself.

For those of you playing along at home, here's the code:

#!/usr/bin/perl

use strict;
use warnings;

use Digest::MD5 qw(md5_hex);

my ($from, $to) = @_;
my (%uniq, $msg);
my ($head, $body);
my $i = 0;

$|++;

open (my $fh, "<$from") || die;
while(<$fh>) {
	if(m/^From /) {
		next if ($msg eq "");
		$i++;
		print '.' if(($i % 50) == 0);
		print " $i\n" if(($i % 1000) == 0);
		($head, $body) = split /\n\n/, $msg;
		$uniq{md5_hex($body)} = $msg;
		$msg = $_;
	} else {
		$msg .= $_;
	}
}

open (my $th, ">$to") || die;
while(my ($k, $v) = each %uniq) {
	print $th $v;
}

Comments (1)

Jonas:

Of course it's PERL. Practical Extraction and Report Language!

About

This page contains a single entry from the blog posted on September 22, 2003 11:16 PM.

The previous post in this blog was Making the Switch.

The next post in this blog is Athena, The Love Affair Begins.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33