帮助perl foreach循环

时间:2011-08-04 16:49:19

标签: perl foreach

我需要帮助确定在哪里拨打我的send_mail ()。我目前将其放在代码中的位置,它会针对每个条件发送一封电子邮件,并且它发送的每封电子邮件都会添加另一条记录作为该集合的一部分。我只对收到的记录发送一封电子邮件感兴趣,显示的是最后一封电子邮件(msg4)。我可以在同一个循环中执行此操作吗?我不确定。

实施例: (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):(仅限于此电子邮件)

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

这是我设置条件的主要部分: (我使用的是Win32 :: OLE包中有一个方法(COLLECTION)。所以它不是数组引用。)

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();
 }

1 个答案:

答案 0 :(得分:2)

将send_email调用移出循环,否则每次循环都会调用它。我假设该函数只发送$ 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();