"Nick Chettle" <[EMAIL PROTECTED]> wrote in message ...
> Hi All,

Hi, Nick.

> #!/usr/bin/perl

This is a very good start. Never forget:

  use warnings;
  use strict;

These are two of the most important lines in the script!!!! :-)
If you had used these simple two lines, you would have found a major problem 
with your script. You are using an entire line of text as an index for an 
array. That doesn't make sense.

The line:

  $msgids[$_] = $&;

attempts to use the entire line contained in the variable $_ as an index for 
the array @msgids. Also, it is more efficient to capture want you want by 
using parentheses in your reg exp and subsequently use $1 instead of $&. Not 
a huge deal but worth noting.

> print "Please enter an e-mail address: ";
> chomp($email = <STDIN>);
>
> open MAILLOG, "/var/log/maillog";
>
> while (<MAILLOG>) {
>     if (/$email/) {
>         if (/[A-Z1-9]{8}/) {
>             $msgids[$_] = $&;
>         }
>     }
> }

Again this is a great start. You definitely don't want to use an array, you 
should use a hash instead. This should solve your delimma.

I am unfamiliar with the format of the Postfix mail log. However, you have 
two choices, you can make the key of your hash the email address or the 
message id. I am assuming that the message id is unique for each message. I 
also assume that the email address is not. Both are good assumptions I hope.

----BEGIN CODE----
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

print "Please enter an e-mail address: ";
chomp(my $email = <STDIN>);
open MAILLOG, "/var/log/maillog";

my %msgids;
# If you want to record only one email address, uncomment
# the next 5 lines
#while (<MAILLOG>) {
#  if ( my($addr) = /($email)/ and my($id) = /([A-Z1-9]{8})/ ) {
#    $msgids{$addr}{$id} = $_;
#  }
#}

# If you want to record all email addresses with the ability
# to go back and look at other email addresses
while (<MAILLOG>) {
  # It is almost impossible to accurately match all valid
  # email addresses. The next line is a very poor attempt. Apologies.
  # See Jeffrey Friedl's "Mastering Regular Expressions" for more info.
  if ( my($addr) = /([\w.&[EMAIL PROTECTED])/ and my($id) = /([A-Z1-9]{8})/ ) {
    $msgids{$addr}{$id} = $_;
  }
}

foreach my $id(keys %{ $msgids{$email} }) {
  print $msgids{$email}{$id};
}
print "\n" x 3;
print Dumper \%msgids;
-----END CODE-----

Please note that if your mail log file is huge, it might take a signicant 
amount of time and memory to add all of its lines to the %msgids hash. You 
have been warned.

Good luck,
ZO 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to