For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2006 > multiline









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author multiline
hawk.alan@gmail.com

2006-10-30, 7:09 pm

open(FILE,$ARGV[0]);

while(<FILE> ){
if (/falls mainly on.*l /s){ print"1";};
print;
}


the rain in spain
falls mainly on the
plain

isn't matching across the line. plz help. The /s modifier doesn't seem
to be
working.

Christian Winter

2006-10-30, 7:09 pm

hawk.alan@gmail.com wrote:
> open(FILE,$ARGV[0]);
>
> while(<FILE> ){
> if (/falls mainly on.*l /s){ print"1";};
> print;
> }
>
>
> the rain in spain
> falls mainly on the
> plain
>
> isn't matching across the line. plz help. The /s modifier doesn't seem
> to be working.


The /s modifier works just fine. But as you're reading in
your file line by line, you never have a line in $_ that
matches your pattern. If you want to read in more than
just one line at once, you should have a look at
perldoc -q "How can I read in an entire file all at once"

And btw., you should always check the return code of open like
open(FILE, $ARGV[0]) or die "Failed to open $ARGV[0]: $!";
and put "use strict;" and "use warnings;" on top of your script.
Even though it might be that an example for this groups seems
too trivial to need this, it might otherwise encourage other
readers to test and post their code without it as well.

HTH
-Chris
hawk.alan@gmail.com

2006-10-30, 7:09 pm

:( .

At least I know now.

It does mean I'm gonna have to change the rest of my script to
accommadate for slurping(ie. $/="").

Dr.Ruud

2006-10-30, 7:09 pm

hawk.alan@gmail.com schreef:

> open(FILE,$ARGV[0]);
>
> while(<FILE> ){
> if (/falls mainly on.*l /s){ print"1";};
> print;
> }
>
>
> the rain in spain
> falls mainly on the
> plain
>
> isn't matching across the line. plz help. The /s modifier doesn't seem
> to be working.


#!/usr/bin/perl -T
use strict ;
use warnings ;

my $fn = $ARGV[0] ;
open my $fh, '<', $fn or die "<$fn> $!" ;

{ # paragraph-mode
local $/ = '' ;

while (<$fh> )
{
/falls mainly on.*l/s and print "found one\n" ;
}
}

close $fh or die "<$fn> $!" ;

--
Affijn, Ruud

"Gewoon is een tijger."

Bart Lateur

2006-10-30, 7:09 pm

hawk.alan@gmail.com wrote:

>It does mean I'm gonna have to change the rest of my script to
>accommadate for slurping(ie. $/="").


Or you could try to use the "range" (aka "flipflop") operator, either 2
or 3 dots. Like this:

while(<FILE> ){
if (my $cnt = (/falls mainly on/ ... /l/)){ print "$cnt ";};
print;
}

It'll match the first part of the pattern and continue until it either
matches the second pattern, or until it reaches the end of the file. So
after the start it'll never fail. You may want to postpone any action
until you're sure the second pattern will match, and meanwhile, buffer
the lines somewhere.

Also be aware that the flipflop won't be switched off at the end of the
file, which means it'll still be on when you read a new file. If that
matters, a reasonable behaviour could be worked out using eof.
--
Bart.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com