Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Split files based on regexp

by druthb (Beadle)
on May 17, 2012 at 11:26 UTC ( #971046=note: print w/replies, xml ) Need Help??

in reply to Split files based on regexp

Here's some meta-code that should show the logic of how I'd approach this:

my $in_combat = 0; #open the file for input LINE: while (my $line=readline($input_file)) { if (# line contains start-combat flag) { $in_combat = 1; } next LINE if (!$in_combat); # do stuff with lines *during* combat--beginning and end will be he +re, too! if (#line contains end-combat flag) { $in_combat = 0; } } # close files, etcetc

TIMTOWTDI, of course.

D Ruth Bavousett

Replies are listed 'Best First'.
Re^2: Split files based on regexp
by roboticus (Chancellor) on May 17, 2012 at 12:11 UTC


    You may want to experiment with the flip/flop operator "..". It basically does the work of your $in_combat logic. It takes a little while to get used to, but it certainly makes the code a bit simpler once you're used to it:

    $ cat #!/usr/bin/perl # # Demo for flip-flop operator # use 5.14.0; use warnings; use autodie; while (<DATA>) { if (/start/ .. /stop/) { # in combat! print "COMBAT: $_"; } } __DATA__ soon the combat will begin start combat! Biff! Pow! Bam! (Looks like Batman!) stop combat fight had three superlatives $ perl COMBAT: start combat! COMBAT: Biff! Pow! Bam! COMBAT: (Looks like Batman!) COMBAT: stop combat


    When your only tool is a hammer, all problems look like your thumb.

      Brilliant! I had not seen that operator before. Thanks oodles, roboticus!

      D Ruth Bavousett

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971046]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2020-12-05 05:51 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (63 votes). Check out past polls.