--------------------------------------------------------------------
4.28: How do I change the Nth occurrence of something?
You have to keep track of N yourself. For example, let's say you want to
change the fifth occurrence of "whoever" or "whomever" into "whosoever"
or "whomsoever", case insensitively. These all assume that $_ contains
the string to be altered.
$count = 0;
s{((whom?)ever)}{
++$count == 5 # is it the 5th?
? "${2}soever" # yes, swap
: $1 # renege and leave it there
}ige;
In the more general case, you can use the "/g" modifier in a "while"
loop, keeping count of matches.
$WANT = 3;
$count = 0;
$_ = "One fish two fish red fish blue fish";
while (/(\w+)\s+fish\b/gi) {
if (++$count == $WANT) {
print "The third fish is a $1 one.\n";
}
}
That prints out: "The third fish is a red one." You can also use a
repetition count and repeated pattern like this:
/(?:\w+\s+fish\s+){2}(\w+)\s+fish/i;
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
[snip solutions]
Recently I noticed another solution for that:
my $str = '1 whomever 2 whoever 3 whomever' .
'4 whoever 5 whomever 6 whoever';
my $n = 5;
my $re = qr/(whom?)ever/;
$str =~ /$re/g for 1 .. 5; # put pos($str) right past fifth match
$str =~ s/$re\G/${1}soever/; # replace that one
Anno
I don't think this actually works. When I try it with the string '1
whomever' it still makes the change. The problem is that pos() isn't
updated for a failed match, but the for() construct doesn't reconginze
when something failed and it just keeps going. By the time you get to
the last s///, you don't really know if pos() is where it should be.
To fix this, you end up with the code in the answer that checks the
success of the match each time.
Oh, you're right. pos() is undefined (which would be fine) after
the /first/ failed match. Then m//g starts another round.
> To fix this, you end up with the code in the answer that checks the
> success of the match each time.
Not quite. Just stop the loop after the first failure, when
pos() is fine:
# put pos($str) right past $n-th match
# leave undef if there aren't $n matches
$str =~ /$re/g or last for 1 .. $n;
# replace that one
$str =~ s/$re\G/${1}SOever/;
That would fix it, but the purpose of "...or last" is obscure. I like
the bogus solution better, it's much clearer :)
Anno
Or, use /gc and stop one short of the match count:
my $n = 5;
my $re = qr/(whom?)ever/;
$str =~ /$re/gc} for 1 .. $n-1; # position at match-1
$str =~ s/\G(.*?)$re/$1${2}soever/ # replace next if any
--
Charles DeRykus
> > To fix this, you end up with the code in the answer that checks the
> > success of the match each time.
>
> Not quite.
> $str =~ /$re/g or last for 1 .. $n;
Well, you are doing just what I said. You check the result of the match
each time. :)
> > To fix this, you end up with the code in the answer that checks the
> > success of the match each time.
>
> Not quite. Just stop the loop after the first failure, when
> pos() is fine:
>
> # put pos($str) right past $n-th match
> # leave undef if there aren't $n matches
> $str =~ /$re/g or last for 1 .. $n;
> # replace that one
> $str =~ s/$re\G/${1}SOever/;
This works quite nicely because the \G anchor is at the end which means
it can't match if pos() is 0, but that's a heck of a lot to understand
or explain in a FAQ :)
ah, very tricky. After the for(), the pos ends up either after any
possible matches in the failure case, or right before the next possible
one.