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

I was wandering if someone could help me do something like this... if I have a file with...

<begin> blah,blah,blah,blah,blah gohere <end>

How can I have it search for the 1st line between <begin> and <end> and then if it finds something go to the next line between those tags and read that in to a variable? I'd really appriciate any help. Thanks.

Ben

Edited 2001-04-04 by mirod: added p and code tags and &lt; and &gt; entities

Replies are listed 'Best First'.
Re: Pattern Matching Question (I think)
by Masem (Monsignor) on Apr 04, 2001 at 17:03 UTC
    It looks like the HTML parser took your tags away (You should make sure to use <CODE> tags to keep things nice.); as well as the fact that it's hard to interpret exactly what you are trying to do. I see either two things that you are attempting:

    Reading something between two tags, such as <TAG>my stuff</TAG>:

    my @array; while ( my $line = <FILE> ) { if ( $line =~ /\<TAG\>(.*)\<\/TAG\>/ ) { push @array, $1; } }
    This matches the text between the two lines, and stores that match (which comes out of the regex as $1), in an array.

    Putting the text of the next line into a variable after matching something on the previous line..., as in

    <TAG>data</TAG> this is data to be stored
    Which would work to be something like...
    my @array; while( my $line = <FILE> ) { if ( $line =~ /\<TAG\>data\<\/TAG\>/i ) { my $data_line = <FILE>; push @array, $data_line; } }
    In this case, if the tag delimiter is matched, then it reads the next line and sticks that into an array.

    If neither case is what you want or helps you get you want to need, you need to be more specific about your format.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Pattern Matching Question (answer, I think)
by OeufMayo (Curate) on Apr 04, 2001 at 17:06 UTC

    Okay, here's my best guess at your problem.

    while (<FILE>){ $line = /<begin>/ .. /<end>/; # do something powerful with $line here }

    Actually, it's a FAQ question.

    Update: Included the <begin/end> tags to reflect the question.

    <kbd>--
    my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>
Re: Pattern Matching Question (I think)
by davorg (Chancellor) on Apr 04, 2001 at 17:05 UTC

    Well, you question as written makes very little sense, but I'm wondering if you want something like this:

    open FILE, 'myfile.txt' or die "Urgh: $!\n"; while (<FILE>) { if (/START TEXT/ .. /END TEXT/) { # Do something with the text in $_ # This will be one line from the file # after the line containing 'START TEXT' # but before the line containing 'END TEXT' } }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Pattern Matching Question (I think)
by suaveant (Parson) on Apr 04, 2001 at 16:55 UTC
    This question doesn't make much sense... did you put < and > chars in your post? give an example of what the data may look like... UPDATE: sorry... to actually put < and > in your post do &lt; or &gt; (less than/greater than)
                    - Ant