Perl Flip-flop operator - Is it possible to treat the END of first match as START of next match?

StackOverflow https://stackoverflow.com/questions/8727631

  •  14-04-2021
  •  | 
  •  

Question

Need some more help on flip-flop operator Below is my sample data:

LS             SPID     ASP            SPID
3-59           MGW05    
SLC ACL PARMG ST                   SDL                             SLI
 0  A1  17    C7STH-1&&-31         MSC19-0/RTLTB2-385
LS             SPID     ASP            SPID
3-618          ETRC18   
SLC ACL PARMG ST                   SDL                             SLI
 0  A2   0    C7ST2C-4             ETRC18-0/RTLTB2-417
 1  A2   0    C7ST2C-5             ETRC18-0/RTLTB2-449
END

The data of interest for me starts from string 'LS SPID ASP SPID' and ends at either next 'LS SPID ASP SPID' or END (if there's no next LS line). Is it possible to get this using flip-flop operator? I read this data into an array (@linesread) and then tried to loop over the array using the below code and it is not working. Is the problem because i cannot loop over the same line twice? Any other solution?

P.S: I am using the ... operator as needed.

foreach (@linesread) {
    if (/^LS\s*SPID\s*ASP\s*SPID$/ ... (/^LS\s*SPID\s*ASP\s*SPID$/ || /^END$/)) {
    print "$. \t $_\n";
    }
}   
Was it helpful?

Solution

The range operator itself cannot be configured to evaluate the left side immediately. But you can try

if (my $r = /^LS\s*SPID\s*ASP\s*SPID$/ ... (/^LS\s*SPID\s*ASP\s*SPID$/ || /^END$/)) {
    print "$. \t $_\n";
    redo if $r =~ /E0$/;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top