"Guo Remy" schreef:
> i'm now trying to match a number of lines in a txt file...for
> example, my txt file is:
>
> abc
> xyz
> fred
> wilma
> barney
> anything
>
> and my code is:
>
> while (<>) {
> if (/(fred\nwilma\n\barney)/) {
The \b is wrong.
> print "$1\n";
> }
> }
>
> but actually it returns nothing...i guess "while (<>)" reads the file
> line by line, so it can not match several lines at a time...right?
>
> so how can i match multiple lines in a file?
> thanks in advance~~ :)
See $/ in perlvar.
See also the m-modifier in perlre, because you'll need
/^fred\nwilma\nbarney$/m.
Or use a state-machine approach:
#!/usr/bin/perl
use warnings ;
use strict ;
local ($\, $,) = ("\n", "\n") ;
my @find = qw(fred wilma barney) ;
my $state = 0 ;
while ( <DATA> )
{
!/^ $find[$state] $/x and --$state ? ($state = 0, next) : redo ;
++$state == @find and print @find and last ; # s/last/$state = 0/
}
__DATA__
abc
xyz
fred
fred
fred
wilma
barney
anything
fred
wilma
barney
pluto
--
Affijn, Ruud
"Gewoon is een tijger."
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>