Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

So I have an old VBS script that access a public folder on MS Exchange, grabs the messages from X minutes ago and looks to see if there are X matches of 3 seperate subjects, then it will send an alert. I'm converting this to Perl because my VBS way is very clunky and prone to breaking due to the way I have to access the folders. With IMAPClient I can just go directly to the folder I need by name. I have it working enough to pull out the subject and dates due to the helpful docs:
for my $h ( # values %{$imap->parse_headers( scalar($imap->search("ALL")) +, "Subject", "Date")} values %{$imap->parse_headers( scalar($imap->search("SUBJECT" +,"A time out or a transaction deadlockDDD")) , "Subject", "Date")} ) { print map { "$_:\t$h->{$_}[0]\n"} keys %$h; }
So I can search via subject, or just retrieve all. But here's where I'm stuck. I need for it to search for 3 different subject lines, and I need to search only X minutes in the past, 30 in this case. If a combined 20 or more matches of the 3 subject lines are found, it fires an alert, otherwise it just loops. I don't understand enough about hashes and keys and map to figure out how to loop through these messages looking for my subject lines. I know I can figure out the go back X minutes from the docs, but the real meat I"m unsure of - any ideas?

Replies are listed 'Best First'.
Re: Using Mail::IMAPClient
by crashtest (Curate) on Nov 08, 2005 at 02:57 UTC

    If you want to get the combined results of searches for three different subjects, you can run each search individually and build one big list to loop over. Your code might look like this:

    my @results1 = values %{imap->... 'SUBJECT', 'Subj1' ...}; my @results2 = values %{imap->... 'SUBJECT', 'Subj2' ...}; my @results3 = values %{imap->... 'SUBJECT', 'Subj3' ...}; my @all_results = (@results1, @results2, @results3); for my $h (@all_results){ # Your code manipulating $h as before... }
    There are much cleaner ways to do this (using map with a list of subjects), but hopefully this makes it clearer what is going on.

    In general, if you want to know the size of an array, you use the array in scalar context. That is to say:

    # sets $num_results to size of @all_results array. my $num_results = @all_results;
    ... which would give you the total number of results your search returned. I'm not sure if this is enough in your case though, as you might need to loop over your results to identify only those dated in the last X minutes.

    Hope this gets you started!
      Thank you very much, that helps me a lot. I just have a couple of questions - if the search doesn't return anything, the script dies with a undefined hash error - I don't know how to handle that one. Also, do you know of some good beginner links where I can learn more about hashes, map, scalar, and other related items? I've googled, but I may just not be getting the terms right as I'm not getting anything that really lays it out there. Well, back to the net and these ORA books :D - thanks again!