by krahnj@[EMAIL PROTECTED]
(John W. Krahn)
May 7, 2008 at 03:46 AM
sanket vaidya wrote:
> HI all,
Hello,
> Kindly go through the code below.
>
> use warnings;
> use strict;
> my $i=1;
> while($i<=10)
> {
> $_ = "abcpqr";
> $_=~ s/(?=pqr)/$i/;
> print "$_\n";
> $i++;
> }
>
> Output:
> abc1pqr
> abc2pqr
> abc3pqr
> abc4pqr
> abc5pqr
> abc6pqr
> abc7pqr
> abc8pqr
> abc9pqr
> abc10pqr
>
> The expected output is
>
> abc001pqr
> abc002pqr
> abc003pqr
> abc004pqr
> abc005pqr
> abc006pqr
> abc007pqr
> abc008pqr
> abc009pqr
> abc010pqr
>
> Can any one suggest me how to get that output using regex. i.e. Can this
> happen by making change in regex I used in code??
$ perl -e'
use warnings;
use strict;
for my $i ( 1 .. 10 ) {
$_ = "abcpqr";
$_ =~ s/(?=pqr)/sprintf q[%03d], $i/e;
print "$_\n";
}
'
abc001pqr
abc002pqr
abc003pqr
abc004pqr
abc005pqr
abc006pqr
abc007pqr
abc008pqr
abc009pqr
abc010pqr
Or maybe this would be better:
$ perl -e'
use warnings;
use strict;
$_ = "abcpqr";
$_ =~ s/(?=pqr)/%03d/;
for my $i ( 1 .. 10 ) {
printf "$_\n", $i;
}
'
abc001pqr
abc002pqr
abc003pqr
abc004pqr
abc005pqr
abc006pqr
abc007pqr
abc008pqr
abc009pqr
abc010pqr
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall