No, as you suspect, s/// doesn't work like that.
How about something like:
use strict; use warnings; my (@config); push @config, 'line 1 oldtext appears but once here', 'line 2 oldtext is here twice oldtext', 'line 3 oldtext is here oldtext three oldtext times!'; for (@config) { my $count = 2; while ($count-- && s/oldtext/newtext/) { print 'Replaced '} print "\n"; } print "$_\n" for @config;
Output:
Replaced Replaced Replaced Replaced Replaced line 1 newtext appears but once here line 2 newtext is here twice newtext line 3 newtext is here newtext three oldtext times!
Update -- some explanation:
The print statement in the while loop is to provide a hint about how this works. We're taking advantage of a couple facts here: (1) s/.../.../ will only perform 1 substitution at a time, and (2) s/.../.../ returns the number of substitutions it performed (either 0 or 1 in this case). I set $count to the maximum number of replacements I want to make.
For the first line of the array, the while loop runs only once. Even though $count at that point is 1, the s/// returns 0, and that makes the while condition false.
For the second and third lines of the array, the loop runs only twice, and that's because $count has counted down to 0.
In reply to Re^3: String substitution inside an array
by GotToBTru
in thread Re^2: String substitution inside an array
by dhawal.mahajan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |