short-hand for same regex for multiple scalars?

A

Alexander Huber

Hi,

Is there a shorter way of writing the following (regex only once on
right-hand side?), basically I want to apply the same regular expression
consecutively to multiple scalars:

$a =~ s/a/b/gs;
$b =~ s/a/b/gs;
$c =~ s/a/b/gs;

Thanks,

Alexander.
 
P

Paul Lalli

Alexander said:
Is there a shorter way of writing the following (regex only once on
right-hand side?), basically I want to apply the same regular expression
consecutively to multiple scalars:

$a =~ s/a/b/gs;
$b =~ s/a/b/gs;
$c =~ s/a/b/gs;

Why not just use a loop?

for my $var ($a, $b, $c) {
$var =~ s/a/b/gs;
}

Paul Lalli
 
B

Brian McCauley

John said:
s/a/b/g for $a, $b, $c;

It should also be noted that the very fact that the OP wants to perform
idenitical processing on several scalar values means he should consider
putting them in a agregate data type rather than separately named
scalars.

s/a/b/g for values %some_hash;
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top