Anish Kumar K. wrote:
Hi I have text file in which I have the below mentioned lines

    This is a test
    start-first
    First Line testing
    end-first
    How are you
    Finally
    start-second
    Tested
    end-second

I have a perl file which reads the above file and stores in the variable say 
$message

#!/usr/bin/perl
use strict;
open INPUT,"fp.txt" || die "File does not exist";
my $message="";
while(<INPUT>)
    {
            $message=$message.$_;
    }
if ($message =~ (/start_first/../end_first/))
    {
           print "The line is $1";
    }


What I expect is that from the variable, $message I wanted to print out this "First Line testing" as in the text file i have specified the pointers.. In Short I wanted to print the lines in between two words in a file/variable.

Below is an example of how the .. operator works. You can find a good description in `perldoc perlop` section "Range Operators"


#!/usr/bin/perl

use strict;
use warnings;

while (defined( my $line = <DATA> )) {
    if ( $line =~ /start-/ .. $line =~ /end-/ ) {
        print "The line is $line";
    }
}


__DATA__ This is a test start-first First Line testing end-first How are you Finally start-second Tested end-second


# Note that the loop above can and often is abbreviated: while (<DATA> ) { if ( /start-/ .. /end-/ ) { print "The line is $_"; } }

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to