Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Replace In a Single Line

by tobyink (Abbot)
on Jan 17, 2013 at 08:33 UTC ( #1013727=note: print w/ replies, xml ) Need Help??


in reply to Replace In a Single Line

In recent versions of Perl, you can do:

$var1 = ($var =~ s/oo//gir);

In pretty much any version of Perl, you can do:

($var1 = $var) =~ s/oo//gi;

Update: the second method is about 35% faster according to my benchmarking. The first one is sometimes more elegant - especially when the variable you're operating on (and don't want to be changed) is $_ such as this map block:

my @without_oo = map { s/oo//rig } @with_oo; # traditional version: my @without_oo = map { (my $x = $_) =~ s/oo//ig; $x } @with_oo;

But in your original example, neither of the two solutions I presented above seems more elegant than the other, so I'd prefer the faster one.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Replace In a Single Line
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013727]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2015-07-30 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls