cjd143SD
cjd143SD

Reputation: 869

help with perl foreach loop

I need help figuring out where to call my send_mail (). Where I currently place it in the code, it sends out an email for every condition and each email it sends it adds another record as part of the set. I'm only interested in sending a single email with the collected records, the last email shown (msg4). Can I do this within the same loop? I'm not sure.

Example: (msg1)

Service: MST Engine  - Stopped - Manual

(msg2):

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual

(msg3):

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual
Service: MST Server  - Stopped - Manual

(msg4): (intersted in only this email)

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual
Service: MST Server  - Stopped - Manual
Service: MST Formatter  - Stopped - Manual

Here is the main piece where I set the conditions: (I'm using Win32::OLE package has a method in(COLLECTION). So its not an array reference.)

foreach my $serv (in $servSet) 
{
    next if $serv->{started};
    my $sname  = $serv->{name};
    my $sstate = $serv->{started};
    my $ssmode = $serv->{startmode};
    $winsvcs .= "Service: $sname  - $servicestate[$sstate] - $ssmode\n";
    send_email();
 }

Upvotes: 0

Views: 161

Answers (1)

gpojd
gpojd

Reputation: 23085

Move the send_email call out of the loop or it will call it every time it goes through the loop. I assume the function just sends the contents of $winsvcs.

my $winsvcs = '';
foreach my $serv (in $servSet) {
    next if $serv->{started};
    my $sname  = $serv->{name};
    my $sstate = $serv->{started};
    my $ssmode = $serv->{startmode};
    $winsvcs .= "Service: $sname  - $servicestate[$sstate] - $ssmode\n";

}
send_email();

Upvotes: 2

Related Questions