in reply to Regexp and substitution question

$1 represent the captures of the last successful pattern match. In this instance, that would be s/one/ONE/. Since it has no captures, $1 and friends are empty. Copy $1 and friends when they still contain what you want, or avoid using $1 and friends completely.
my $line = "One two three four five"; if (my @captures = $line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i) + { $captures[0] =~ s/one/ONE/i; print "$_: $captures[$_-1]\n" for 1..5; }

Note that uc might be more appropriate here. Any maybe split.

my $line = "One two three four five"; if (my @captures = split(' ', $line)) { $captures[0] = uc($captures[0]); print "$_: $captures[$_-1]\n" for 1..5; }

Replies are listed 'Best First'.
Re^2: Regexp and substitution question
by bdalzell (Sexton) on Aug 28, 2010 at 14:27 UTC

    Thank you, every one who took the time to answer this. The exercise of writing the request resulted in my realizing when I awoke this AM that the $1, etc involved the most recent match BUT the explanations you provided are instructive beyond the answer to my query and I have learned something that will further improve my program.