On Aug 26, 2005, at 0:43, Jose Malacara wrote:
Can someone please provide some assitance with a multi-line matching
problem? I have a datafile that looks like this:
#### Input file ########
START
foo1
bar1
END
START
foo2
bar2
END
START
foo3
bar3
END
I am trying to capture the contents between the START and END
delineators. Here is what I have so far:
#### Script ########
#!/usr/bin/perl
$input_file = $ARGV[0];
open (INPUT, "<$input_file") or die "Cannot open $input_file file:
$!";
local $/;
while (<INPUT>){
if ( $_ =~ m/^\s*START\s+(.+?)\s+END$/smi ) {
print "$1\n";
}
}
close (INPUT);
However, when I run the script it only appears to match the START/END
sequence the first time. This is what I get:
Yes, since $/ is undef, the first call to <INPUT> puts the entire
file into $_, we enter the while only ONCE in consequence, and in the
while block we just do a m//. That's why you get only one match,
there's no looping going on.
A possible fix:
local $/;
my $contents = <INPUT>;
while ($contents =~ m/^\s*START\s+(.+?)\s+END$/smig) {
print "$1\n";
}
Just for the record, the so-called flip-flop operator is really handy
for this kind of files. The flip-flop operator is ".." in scalar
context, and it is documented in perlop.
-- fxn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>