T
TonyV
Hi all,
If I want to assign the result of a substitution in $_ to a variable,
I can do something like this:
foreach (@foo) {
if (m/old text/) {
($a = $_) =~ s/old/new/; # Assign 'new text' to $a
}
# Do more stuff...
}
Of course, one of the nice things about using $_ is that it is the
default variable used in operations such as matches and
substitutions. I'd like to be able to do something like this instead:
foreach (@foo) {
if (m/old text/) {
$a = s/old/new/; # This does not work
}
# Do more stuff...
}
What's happening is that the substitution is occurring in $_ and the
number of substitutions (1, in this case) is being assigned to $a. Is
there some more elegant way to assign the result of the substitution
to $a without explicitly assigning $_ to it?
If I want to assign the result of a substitution in $_ to a variable,
I can do something like this:
foreach (@foo) {
if (m/old text/) {
($a = $_) =~ s/old/new/; # Assign 'new text' to $a
}
# Do more stuff...
}
Of course, one of the nice things about using $_ is that it is the
default variable used in operations such as matches and
substitutions. I'd like to be able to do something like this instead:
foreach (@foo) {
if (m/old text/) {
$a = s/old/new/; # This does not work
}
# Do more stuff...
}
What's happening is that the substitution is occurring in $_ and the
number of substitutions (1, in this case) is being assigned to $a. Is
there some more elegant way to assign the result of the substitution
to $a without explicitly assigning $_ to it?