Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing arrays 1

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I'm having a lot of trouble with this one part my script. I have a script that reads the output of a command (on a Linux system) and does a regexp to sort each field into a variable. It then sends emails out if certain conditions are met (if the email address field exists and if the last field is 'true').

The problem is that I need to store which emails have already been sent out so that users don't get duplicates. I was thinking about using the Storable module to store each email I send out in a text file. I thought the easiest way would be to just store the sequence number of the email ($seq) in the text file, and then check the text file each time to make sure it hasn't already sent that one out yet. Here's the code, minus the Storable part that I can't get working properly.

Code:
#!/usr/bin/perl -w
use Net::SMTP;

@vfx = `vfxolog -f seq,fem,sub,tnm,tfn,dia,ars,rrs,sbt,csi,nps,don`;

foreach (@vfx) {
  $_ =~ /"(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)","(.*)"/;
  $seq = $1;
  $email = $2;
  $subject = $3;
  $name = $4;
  $tofax = $5;
  $dial = $6;
  $faxattempt = $7;
  $faxresult = $8;
  $time = $9;
  $csi = $10;
  $nps = $11;
  $don = $12;

  if ($tofax) { substr $tofax, 3, 0, "-"; }

  if ($dial) { substr $dial, 3, 0, "-"; }

  if ($csi) { $csi =~ s/ /-/g; }

  if ($time) {
   $year = substr($time,0,4);
   $month = substr($time,4,2);
   $day = substr($time,6,2);
   $hour = substr($time,8,2);
   $minute = substr($time,10,2);
   $date = $month . '/' . $day . '/' . $year . ' ' . $hour . ':' . $minute;
  }

  if ($don and $don eq 'true') { $emailsubject = "VSI-Fax Job $seq Completed"; }

  if ($email and $don eq 'true') {
   $mailserver = '192.0.1.2';
   $smtp = Net::SMTP->new($mailserver);
   $smtp->mail('VSI-Fax@foo.org');
   $smtp->to($email);
   $smtp->data();
   $smtp->datasend("Subject: $emailsubject\n");
   $smtp->datasend("
Sequence Number:       $seq
Email address:         $email
Subject:               $subject
Name:                  $name
Fax number:            $tofax
Dial string:           $dial
Fax attempt status:    $faxattempt
Fax result status:     $faxresult
Submit time:           $date
CSI Number:            $csi
Number of Pages:       $nps
\n");
   $smtp->dataend();
   $smtp->quit;
  }
}


I was trying to do something like this, but it didn't work...

Code:
# at the top of the script...
use Storable;
$file = '/root/faxjobs.txt';
$fax_ref = retrieve($file);
@old = @$fax_ref;

# towards the bottom of the script...
if ($email and $don eq 'true') {
   foreach $old (@old) {
     if ($seq != $old) {
       ## then email it like above and store it in a new array
       push @new, $seq;
      }
   }
 }

@completed = (@old, @new);
store \@completed, $file;


Thanks a lot if anyone can help me with this, its driving me crazy.

Chris


 
I should include an example of what the vfx command produces...

"1007","jdoe@foo.com","RHCE Exam Results","John Doe","5823293","5823293","NORMAL","NORMAL","20040610081834","1 631 555 3293","3","true"
"1006","csmith@foo.com","SOME RANDOM SUBJECT","Chris Smith","5823293","5823293","NORMAL","NORMAL","20040610080918","1 631 555 3293","3","true"
"1005","","","","5553293","5553293","NORMAL","NORMAL","20040609170313","1 631 555 3293","3","true"
"1004","blee@foo.com","","","5553293","5553293","RETRY","RETRY","20040609163932","1 631 555 3293","9","false"
"1003","","","","5553293","5553293","NORMAL","NORMAL","20040609161427","1 631 555 3293","2","true"
"1002","","","","5553293","5553293","NORMAL","NORMAL","20040609145526","1 631 555 3293","8","true
 
I wrote a longer post, then lost it. Here's the code, it might be what you want.


Code:
  if ($email and $don eq 'true') {
      [highlight]unless ($emailsent ~= /$email/) {[/highlight]
         $mailserver = '192.0.1.2';
         $smtp = Net::SMTP->new($mailserver);
         $smtp->mail('VSI-Fax@foo.org');
         $smtp->to($email);
         $smtp->data();
         $smtp->datasend("Subject: $emailsubject\n");
         $smtp->datasend("
Sequence Number:       $seq
Email address:         $email
Subject:               $subject
Name:                  $name
Fax number:            $tofax
Dial string:           $dial
Fax attempt status:    $faxattempt
Fax result status:     $faxresult
Submit time:           $date
CSI Number:            $csi
Number of Pages:       $nps
\n");
         $smtp->dataend();
         $smtp->quit;
         [highlight]$emailsent = $emailsent . " $email";[/highlight]
      [highlight]}[/highlight]
  }

That way, when you send an email you add the recipient to a 'list'. So, each time the conditions are met to send an email, you first check to see if you've already sent them one, and if you have, then you don't send it. I hope I didn't misunderstand.
 
hey dozier, thanks for the reply, but I don't think this is what I'm looking for.

Everytime I send a user an email, I want to record the sequence number ($seq) in a text file somewhere. Whenever the script is run, it will open that text file and check to see whether or not an email for that sequence number has already been sent out or not so that users don't get duplicate emails. This means that whenever an email is sent, the sequence number must then be added to the text file to avoid duplicate emails in the future.

Am I making any sense right now?

Thanks,
Chris
 
So it has to maintain this information of which sequence numbers have already had emails sent out between occurances of the script being run? If that's the case, then it sounds like you have the philosophy down for a method of doing it. I personally don't know anything about using Storable, and I'm not sure what the benefit of that is over using standard file i/o.

at the beginning of the script open the file and read the seq #'s into an array:
Code:
open SFL, "</root/faxjobs.txt";
while <SFL> {
   chomp;
   push @sequencenums, $_;
}
close SFL;

and then:
Code:
  if ($email and $don eq 'true') {
      [highlight]$seq_sent = 0;
      foreach (@sequencenums) {
         if ($_ ~= /$seq/) {
            $seq_sent = 1;
         }
      }
      unless ($seq_sent == 1) {[/highlight]
         $mailserver = '192.0.1.2';
         $smtp = Net::SMTP->new($mailserver);
         $smtp->mail('VSI-Fax@foo.org');
         $smtp->to($email);
         $smtp->data();
         $smtp->datasend("Subject: $emailsubject\n");
         $smtp->datasend("
Sequence Number:       $seq
Email address:         $email
Subject:               $subject
Name:                  $name
Fax number:            $tofax
Dial string:           $dial
Fax attempt status:    $faxattempt
Fax result status:     $faxresult
Submit time:           $date
CSI Number:            $csi
Number of Pages:       $nps
\n");
         $smtp->dataend();
         $smtp->quit;
         [highlight]push @sequencenums, $seq;
      }[/highlight]
  }

and then before the script exits, write the new set of seq #'s to the file:
Code:
sub write_seq {
   open SFL, ">/root/faxjobs.txt";
   foreach (@sequencenums) {
      print SFL "$_\n";
   }
   close SFL;
}

Is that what you were thinking?
 
Thanks a lot dozier, it works great. It was driving me crazy trying to get that to work last week.
 
is this why your script was not working?

Code:
# at the top of the script...
use Storable;
$file = '/root/faxjobs.txt';
$fax_ref = retrieve($file);
@old = @$fax_ref;

# towards the bottom of the script...
if ($email and $don eq 'true') {
   foreach $old (@old) {
     if ($seq [b][red]ne[/red][/b] $old) {
       ## then email it like above and store it in a new array
       push @new, $seq;
      }
   }
 }

@completed = (@old, @new);
store \@completed, $file;


Kind Regards
Duncan
 
duncdude, I just tried but it didn't work with 'ne' either for some reason. No big deal though, dozier's script works fine. I am a little curious as to why it doesn't work though.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top