Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

read between two strings

by harry34 (Sexton)
on May 16, 2002 at 14:49 UTC ( #167019=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
How do I tell perl to specifically read the numbers inbetween
%%Atom Coords and %%End Frame in the following pattern.

%%Atom Coords
-1.13 12.16 6.15
%%End Frame

cheers harry

Replies are listed 'Best First'.
Re: read between two strings
by Rhose (Priest) on May 16, 2002 at 15:07 UTC
    You could use the flip-flop operator to skip lines which are not between the %%Atom Coords and %%End Frame markers. Maybe something like this?

    #!/usr/bin/perl -w use strict; while (<DATA>) { #-- Skip lines not in the section to be processed next unless /^%%Atom Coords/../^%%End Frame/; #-- Skip the section markers next if (/^%%Atom Coords/ or /^%%End Frame/); #-- Parse lines... print; } __DATA__ %%Atom Coords -1.13 12.16 6.15 %%End Frame more stuff... and more stuff... %%Atom Coords -2.26 24.32 12.30 %%End Frame
      #-- Skip lines not in the section to be processed next unless /^%%Atom Coords/../^%%End Frame/; #-- Skip the section markers next if (/^%%Atom Coords/ or /^%%End Frame/);
      It's bad coding practice to write and test those twice, and you don't need to, if you pay attention to the return value:
      my $inrange = /^%%Atom Coords/../^%%End Frame/; next unless $inrange; next if $inrange == 1 or $inrange =~ /E/; # skip first and last item + of range
      Very simple. RTFM.

      -- Randal L. Schwartz, Perl hacker

(wil) Re: read between two strings
by wil (Priest) on May 16, 2002 at 15:07 UTC
    I'm not the best when it comes to regex's, but I would use something like this:
    my $s = qq|%%Atom Coords -1.13 12.16 6.15 %%End Frame|; $s =~ s/^%%Atom Coords\s*(.*)\s*%%End Frame$/$1/; print $s;
    May I also suggest (and I will suggest this to myself, too ;-)) that you pick up a good book on this subject.

    - wil

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://167019]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2023-05-30 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?