in reply to Perl code help

Hello usertest,

As I understand it, you want to retain all lines that do not come between START and END, and also those that do whenever def doesn’t appear in the same block; but when def does occur between START and END, you want to exclude that entire block of data.

If this is correct, then you need to store all the lines in each START ... END block (e.g., in an array), and print them if and only if it turns out that the block doesn’t contain def. For keeping track of whether you’re currently in a block, the range operator in scalar context is the most appropriate tool:

#! perl use strict; use warnings; my ($found, @lines); while (<DATA>) { if (my $flag = /START/ .. /END/) { $found = /def/ unless $found; push @lines, $_; if ($flag =~ /E0$/) { if ($found) { $found = 0; } else { print for @lines; } @lines = (); } } else { print; } } __DATA__ Source Data START abc def ghi END START xyz abc END

Output:

14:22 >perl 1646_SoPW.pl Source Data START xyz abc END 14:22 >

For the range operator, see perlop#Range-Operators.

Update: Fixed logic error in code by moving the @lines = (); statement to below the inner if ... else block; also improved wording slightly.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Perl code help
by usertest (Initiate) on May 29, 2016 at 10:20 UTC
    Thanks Athanasius, this helped a lot. Also, I need one more help. What if the START and END tags are not as simple - it maybe starting with many spaces and instead of START - it will be
    START STEP abc absbsdefdbdb ghi END STEP START STEP xyz abc END STEP
    I need the same result to be generated

      Hi usertest,

      What if the START and END tags are not as simple

      Did you try out the code that Athanasius posted above? Was the output as you expected it or not?

      The regular expressions that Athanasius used will match the words "START" and "END" anywhere in the line, so at least with this sample data you posted it works the same.

      why this statement $flag =~ /E0$/ and how it works

      This regular expression tests for a special value returned by the range operator in scalar context aka flip-flop operator: it marks the end of a range (see the link for all the details). That means that in this case, the condition is true when an "END" is encountered, then the code checks whether it $found a "def" string and acts appropriately, either printing the lines it captured or not.

      Hope this helps,
      -- Hauke D