perl中的数据块未被选中

时间:2016-07-19 07:39:14

标签: perl

如果我从文件中获取数据,下面的代码工作正常 但是如果我在数据块中有相同的数据,那确实有效吗? 这个脚本的座右铭是搜索780222306003的数组,它以模式My BEING REMOVED开头 并搜索' up.v.000652',780222304203,如果它以模式LINK BEING REMOVED开头

是否建议更改代码?

use Switch; 
use strict ; 
use warnings; 

use Data::Dumper; 

my $chunk_separator=q(); 
local $/ = $chunk_separator; 
getFirstIntervalReport('MY_W',780222306003,'up.v.000652',780222304203); 


sub getFirstIntervalReport 
{ 
    my ($wssmInstType, $expLinkMy, $expStation,$expectedMyFailure) = @_; 
    my $scan = "on"; 
    # Sort the report files basd on the date in the file name and process each file 
        my @fileData = <DATA>; 
        foreach my $input_line (@fileData) 
        { 
            print "$input_line \n"; 
           my @my_fields =  split /\n/, $input_line ; 
           my $info_containing_My = grep  /My BEING REMOVED/ , $my_fields[0]; 
           my $info_containing_link = grep  /LINK BEING REMOVED/ , $my_fields[0]; 
           if( $info_containing_My ) { 
              print Dumper @my_fields; 
              if( grep  /$expectedMyFailure/ , $my_fields[1]){ 
                print Dumper @my_fields; 
              } 
              else { print "Expected My not deleted \n "; } 
           } 

           elsif ( $info_containing_link && $scan eq "on" ){ 
              if((grep  /$expLinkMy/ , $my_fields[2]) &&  (grep /$expStation/ ,$my_fields[3])){ 
                 $scan = "off" ; 
                 print Dumper @my_fields; 
              } 
              else { 
                  $scan = "on" ; 
                  next; 
              } 
           } 
       } 
       if($scan eq "on") { print " $expLinkMy, $expStation Not Found \n"} 
       } 




__DATA__ 
<ALERT> ID=7 Link Straight info Loss with count of 10 for 
        My Identifier:780222304404 
        Station Address:up.v.000654 
Time: 1468581106 

<ALERT> ID=17 Link Straight info Loss with count of 10 for 
        My Identifier:780222304203 
         Station Address:up.v.000657 
Time: 1468581994 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304404 
         Station Address:up.v.000654 
Time: 1468582162 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222306003 
         Station Address:up.v.000654 
Time: 1468582562 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
         Station Address:up.v.000301 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
         Station Address:up.v.000654 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
         Station Address:up.v.000656 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
         Station Address:up.v.000657 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
        Station Address:up.v.000656 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222304203 
        Station Address:up.v.000657 
Time: 1468583082 

<INFO>  LINK BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
for 
        My Identifier:780222306003 
         Station Address:up.v.000652 
Time: 1468583578 

<INFO>  My BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
        My Identifier:780222304203 
Time: 1468583082 

<INFO>  My BEING REMOVED: Link has had no communication for an extended period of time - being removed from monitoring 
        My Identifier:780222304454 
Time: 1468583082

https://stackoverflow.com/a/38452025/6594367

2 个答案:

答案 0 :(得分:4)

use Switch__DATA__块存在已知问题。 删除use Switch即可。

完全看到relevant perlmonks question。显然它与Switch模块过滤源的方式有关。

答案 1 :(得分:0)

您可能想看看这个问题。我认为它与你需要的相似。 https://stackoverflow.com/a/38452025/6594367

适应您的情况:

use strict;
use warnings;

my $var;
{    
    local $/;
    $var = <DATA>;
}
my @array = split ( /\n\n/, $var); # your information is separated by double \n
use Data::Dumper;
print Dumper @array;

Here is your content from __DATA__

从这里你可以解析你的元素:

my @index_containing_My = grep { /My\sBEING\sREMOVED/ } @array;
....