Assignments with $_ using substitution

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?
 
J

Jürgen Exner

TonyV said:
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?

Hmmm, well, you can execute the substitution on a different variable by
using the binding operator.

$a =~ s/old/new/;

Details see 'perldoc perlop'
That is not exactly what you asked for, but I think it is what you meant to
ask. My appologies if I interpreted your question the wrong way and you were
actually asking for an explicit assignment.

jue
 
G

Gunnar Hjalmarsson

TonyV said:
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?

Don't think so.

($a = $_) =~ s/old/new/;

is a short form of

$a = $_;
$a =~ s/old/new/;
 
M

Michele Dondi

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?

Well, not really. C<s///> applies a substitution to the variable it
is... well, applied to. You will have to wait for Perl 6's .subst
method to do differently. In the meanwhile, other than using a lexical
variable, you can play with C<local $_> (with a caveat) or with
C<for>'s aliasing properties, which become convenient if two or more
regex-like operators are applied.


Michele
 
M

Mirco Wahab

TonyV said:
... is that the substitution is occurring in $_ and the
number of substitutions (1, in this case) is being assigned to $a.

which is a documented behaviour.
Is there some more elegant way to assign the result
of the substitution to $a without explicitly assigning $_ to it?

You probably think of 'return a copy of the substitution'
directly to a new variable, like:

...
sub substitute {(my $t=$_) =~ s/$_[0]/$_[1]/; $t}
...

...
my @foo = qw{ don't even think about it};

foreach (@foo) {
if( /n/ ) {
my $var = substitute 'n', 'nn'; # does work
print $var,"\n"
}
}
...

Regards

M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top