darrengan has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow...
I found some solution to get data between START and END from a file.
while (<file>) { next unless /START/ .. /END/; print $_; }
But this code will grab everything from START to END E.G.

START
1
2
3
4
END

How do i get the data without the START and END? e.g
1
2
3
4
Cheers mate...
Darren
Florist In Malaysia

Replies are listed 'Best First'.
Re: Between START .. END
by merlyn (Sage) on Sep 14, 2005 at 02:15 UTC
    The value returned by scalar ".." is the sequential count, with "E0" tacked on the final line. So, just save it and use it:
    while (<file>) { next unless my $where = /START/ .. /END/; print unless $where == 1 or $where =~ /E0/; }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Hie Merlyn,
      That reply was quick!
      Tested the code and it works fine...
      Cheers to u!

      Florist In Malaysia

Re: Between START .. END
by kprasanna_79 (Hermit) on Sep 14, 2005 at 07:16 UTC
    Hi darrengan,
    Please go through this link in perlmonks site to get more info on this Scalar Range Operator
    -Prasanna.K

    Edit by castaway - localised link

Re: Between START .. END
by ioannis (Abbot) on Sep 14, 2005 at 11:28 UTC
    # if START and END occur only once, we could lock it with m??
    while (<DATA>) { next unless my $count = ?start?../end/; next if $count == 1; print; }
      Here's a regex that would do the trick.

      #!/usr/bin/perl -w use strict; while (<DATA>) { $_ =~ tr/!@#$%^&*//d; # will you encounter funky chars? next unless ($_ =~/(^\d+)/); print $_; } __DATA__ START 1 2 3! 4 5 END START 1* 2 3 4 5 6 END
Re: Between START .. END
by Roy Johnson (Monsignor) on Sep 14, 2005 at 17:46 UTC
    while (<file>) { next if (/START/../END/) =~ /^1?$|E/; print; }

    Caution: Contents may have been coded under pressure.
Re: Between START .. END
by darrengan (Sexton) on Oct 06, 2005 at 08:08 UTC
    Hie friends,
    Now i can get the data in between <START> and <END>
    How do i abort the script if there is no <END>?

    Cheers,
    Darren

      Now i can get the data in between <START> and <END>
      What do you mean with "get"? (Taking into account merlyn's answer) would something like
      push @lines, $_ unless $where == 1 or $where =~ /E0/;
      do?
      How do i abort the script if there is no <END>?
      IIUC you can realize that there's no end "tag" only when you terminate processing your input file. You may just maintain a flag:
      my $foundend; while (<file>) { next unless my $where = /START/ .. ($foundend=/END/); # ... } print "found\n" if $foundend.