in reply to Regex for simple parsing job

Wow, a perfect use of the scalar range operator.

#! /usr/bin/perl -w use strict; my @titles; while (<DATA>) { if (my $num = /TITLE/ .. /ENDTITLE/) { push @titles, $_ unless $num == 1 || $num =~ /E/; } } print for @titles; __DATA__ STARTP TITLE some gibberish some more gibberish ENDTITLE TITLE some gibberish some more gibberish ENDTITLE TITLE some gibberish some more gibberish ENDTITLE ENDP STARTP TITLE some gibberish some more gibberish ENDTITLE TITLE some gibberish some more gibberish ENDTITLE TITLE some gibberish some more gibberish ENDTITLE ENDP

Replies are listed 'Best First'.
Re^2: Regex for simple parsing job
by Art_XIV (Hermit) on Jul 27, 2004 at 13:24 UTC

    Although I like a spicy regex as much as the next guy, I think that the use of the flip-flop operator, as pointed out by pbeckingham and diebyz is much more elegant and (probably) more efficient, since it allows you to avoid slurping the file that is being parsed.

    The 'scalar range' / 'flip-flop' operator is one of the sweetest pieces of syntactical sugar that Perl offers, if you ask me.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
      For the first Part From STARTP to ENDP. But as I stated previous not for the TITLE part, because there is no endtag. But I'm going to use the flip flop operator because the file can be very large.

      Thanx for pointing me to this!!!



      --
      My opinions may have changed,
      but not the fact that I am right