M
Mahesh Asolkar
Greetings,
I am trying to pick parts of a text file, process them and put them
back in place. The parts to be processed are marked by some begin and
end patterns.
Following is the essence of what I have.
-----
#!/usr/bin/perl
use strict;
use warnings;
local $/;
local $_ = <DATA>;
my $post;
while (/\s*(begin|end)\s*/) {
my ($pre, $mat) = ($`, $&);
$post = $';
print "" . ($mat =~ /begin/)
? " $pre\n"
: "--> " . uc($pre) . "\n";
#
# Any alternative to the following?
#
$_ = $post;
}
print " $post";
__DATA__
other text1 begin part1 end other text2
begin part2 end other text3 begin part3
end other text4 begin part4 end other text5
-----
The above script generates results as I want - it picks parts between
'begin' and 'end', processes them (upcasing) and prints the data in the
original flow. The data is not printed exactly as in the data source
for debugging purposes:
-----
% script.pl
other text1
--> PART1
other text2
--> PART2
other text3
--> PART3
other text4
--> PART4
other text5
-----
Although, I am wondering if there is a way to do away with the '$_ =
$post' line.
I tried to use m//g modifier to remember the position of last match and
resume from there. But then I get a PREMATCH that begins at the
begining of the entire string, not where the matching resumed.
-----
#!/usr/bin/perl
use strict;
use warnings;
local $/;
local $_ = <DATA>;
my $post;
while (/\s*(begin|end)\s*/g) {
my ($pre, $mat) = ($`, $&);
$post = $';
print "" . ($mat =~ /begin/)
? " $pre\n"
: "--> " . uc($pre) . "\n";
}
print " $post";
__DATA__
other text1 begin part1 end other text2
begin part2 end other text3 begin part3
end other text4 begin part4 end other text5
-----
Any suggestions?
Thanks,
Mahesh.
I am trying to pick parts of a text file, process them and put them
back in place. The parts to be processed are marked by some begin and
end patterns.
Following is the essence of what I have.
-----
#!/usr/bin/perl
use strict;
use warnings;
local $/;
local $_ = <DATA>;
my $post;
while (/\s*(begin|end)\s*/) {
my ($pre, $mat) = ($`, $&);
$post = $';
print "" . ($mat =~ /begin/)
? " $pre\n"
: "--> " . uc($pre) . "\n";
#
# Any alternative to the following?
#
$_ = $post;
}
print " $post";
__DATA__
other text1 begin part1 end other text2
begin part2 end other text3 begin part3
end other text4 begin part4 end other text5
-----
The above script generates results as I want - it picks parts between
'begin' and 'end', processes them (upcasing) and prints the data in the
original flow. The data is not printed exactly as in the data source
for debugging purposes:
-----
% script.pl
other text1
--> PART1
other text2
--> PART2
other text3
--> PART3
other text4
--> PART4
other text5
-----
Although, I am wondering if there is a way to do away with the '$_ =
$post' line.
I tried to use m//g modifier to remember the position of last match and
resume from there. But then I get a PREMATCH that begins at the
begining of the entire string, not where the matching resumed.
-----
#!/usr/bin/perl
use strict;
use warnings;
local $/;
local $_ = <DATA>;
my $post;
while (/\s*(begin|end)\s*/g) {
my ($pre, $mat) = ($`, $&);
$post = $';
print "" . ($mat =~ /begin/)
? " $pre\n"
: "--> " . uc($pre) . "\n";
}
print " $post";
__DATA__
other text1 begin part1 end other text2
begin part2 end other text3 begin part3
end other text4 begin part4 end other text5
-----
Any suggestions?
Thanks,
Mahesh.