Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Your problem is that you are reading the file line by line but expect three lines in your search pattern

There are a lot of ways you could do this, one would be to use a state machine. It uses a variable that notes which state it is in and depending on the next line switches to an appropriate state.

In this case state 1 means "I'm at the line after the first line I'm looking for" and state 2 means "I'm after the second line which followed the first line, expecting the number now"

#!/usr/bin/perl use strict; use warnings; my @array=(); open (INF, "$file"); my $state= 0; while (<INF>){ if ($state==0) { if (/^First line of text$/) { $state=1; } next; } if ($state==1) { if (/^second line of text$/) { $state=2; } elsif (/^First line of text$/) { $state=1; } else { $state=0; } next; } if ($state==2) { if (/^in the third line I have (\d*\.\d*)/) { push(@array,$1); $state=0; } elsif (/^First line of text$/) { $state=1; } else { $state=0; } next; } }

Note I used ^ and $ to denote line begin and end in the patterns so that the full lines must correspond to the pattern, not only a part of a line. Also used strict and warnings.

In reply to Re: multi-line parsing by jethro
in thread multi-line parsing by amit223

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2020-08-09 12:01 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (54 votes). Check out past polls.