Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: [Solved]Need to extract a particular block of lines between two patterns

by sundialsvc4 (Abbot)
on Nov 09, 2017 at 01:54 UTC ( #1202992=note: print w/replies, xml ) Need Help??


in reply to [Solved]Need to extract a particular block of lines between two patterns

If this code works for you – if strenuous testing reveals that it does in fact work in all cases (I suspect that it doesn’t ...) – then of course count your blessings and move on.   But I would much rather see logic that very clearly expresses the actual algorithm and does so in a way that can be easily tested and patched.

To my way of thinking, the logic is a classic Finite-State Automaton (FSA) which at any time can be said to be in one of three “states”:

  1. Is not within a block of lines that should be considered for extraction.
  2. Is in a block of lines that should be extracted.
  3. Is in a block of lines that should not be extracted.
Furthermore, the transition between states #2 and #3 “flip-flops” each time.

One way to do something like this – and this is un-tested – would look something like this, specifically including all comments:

my $in_a_block = false; my $extract_me = false; // state for FIRST block while (<$fh_r>) { if (/^START$/) { $in_a_block = true; } elsif (/^END$/) { $in_a_block = false; $extract_me = ( $extract_me ? false : true ); // for NEXT time } elseif ($in_a_block && $extract_me) { // do something with the line ($_) } }

My argument for this approach is that one can readily see how it works and that it does (I think ...) work.   Furthermore, if when the logic needs to be changed, perhaps by someone else, it is possible to do it with relatively little disruption.

And, I would certainly insist that anyone on my team must create a series of automatic (Test::Most) tests to demonstrate that the logic actually does work and that, over time, it continues to do so.   This would establish that the solution is correct, that it is trustworthy, and that any future changes which introduced bugs would be quickly caught.

  • Comment on Re: [Solved]Need to extract a particular block of lines between two patterns
  • Download Code

Replies are listed 'Best First'.
Re^2: [Solved]Need to extract a particular block of lines between two patterns
by Laurent_R (Canon) on Nov 09, 2017 at 09:00 UTC
    Hi sundialsvc4,

    chengchl's question was essentially: how can I use the flip flop operator and pick up only the second START .. END block of text.

    Even assuming your suggested code were correct Perl code (which it isn't) and would run, it still wouldn't do what the OP was asking for, but only mimic the flip flop operator.

    The flip flop operator has a somewhat surprising edge-case behavior (when exactly is it reset) which might justify writing explicit code, but your code (if it worked) would not do what was asked for.

    Update: s/would do what/would not do what/;. Thanks to hippo for noticing the missing negation at the end of my last sentence above. Now fixed.

Re^2: [Solved]Need to extract a particular block of lines between two patterns
by eyepopslikeamosquito (Chancellor) on Nov 09, 2017 at 21:43 UTC

        Why do you or so much energy into replying? Everything was already said, just link to an overview post summing it up.

        I don't share your confidence that "everything was already said". Though I could link to a node that summarises the observed behaviour patterns, that does not solve the mystery of the "why" behind the behaviour. Note that your summary link does not address the "why" behind the behaviour.

        Please note that -- unlike many of his detractors -- sundial does not (deliberately) hide behind a cloak of anonymity, so simplistic "psychology of internet troll" arguments do not apply. If anyone with psychology qualifications can provide some insight, I'm all ears.

Re^2: [Solved]Need to extract a particular block of lines between two patterns
by Anonymous Monk on Nov 09, 2017 at 02:19 UTC
    Thank you for your javascript solution to a perl question. When I do run it in perl I get:
    Bareword found where operator expected near "// state" elseif should be elsif syntax error near "// state " Bareword "false" not allowed while "strict subs" in use Bareword "false" not allowed while "strict subs" in use Bareword "false" not allowed while "strict subs" in use Bareword "true" not allowed while "strict subs" in use
Re^2: [Solved]Need to extract a particular block of lines between two patterns
by Anonymous Monk on Nov 09, 2017 at 09:25 UTC

    And, I would certainly insist that anyone on my team must create a series of automatic (Test::Most) tests to demonstrate that the logic actually does work
    I know someone in your team who doesn't test their code

Re^2: [Solved]Need to extract a particular block of lines between two patterns
by Anonymous Monk on Nov 09, 2017 at 10:38 UTC
    Code that does not even compile, trash words, nothing of value, as usual. The ravings of a sad, mad old man.

      The ravings of a sad, mad old man.

      Be nice. Your time is gonna come.


      The way forward always starts with a minimal test.

        Be nice. Your time is gonna come.

        Why are you defending spam ? Just cause the guy spammed you in real life? We dont need spam here

      Spam is spam
Re^2: [Solved]Need to extract a particular block of lines between two patterns
by Anonymous Monk on Nov 09, 2017 at 10:23 UTC

    If this code works for you ... NOT PERL

    Go away

    Mike Robinson unqualified to give perl advice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202992]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-19 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (114 votes). Check out past polls.

    Notices?