Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Replace In a Single Line

by anshumangoyal (Scribe)
on Jan 17, 2013 at 08:25 UTC ( #1013726=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I have a Variable
$var = "GoodMan"
I want to replace "oo" and assign it to a variable $var2 in a single line. With Multiple Lines I can do like this:
$var1 = $var; $var1 =~ s/oo//gi;
How to do this in a line, so that $var is not changed and $var1 get's changed variable.

Replies are listed 'Best First'.
Re: Replace In a Single Line
by tobyink (Abbot) on Jan 17, 2013 at 08:33 UTC

    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'
Re: Replace In a Single Line
by 7stud (Deacon) on Jan 17, 2013 at 11:04 UTC
    use strict; use warnings; use 5.012; my $var = "GoodMan"; my $result = join "", (sprintf "%s", $var) =~ m/(.*?) oo (.*)/xms; say $var; say $result; --output:-- GoodMan GdMan

    A ridiculous question deserves a ridiculous answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013726]
Approved by tobyink
Front-paged by Corion
help
Chatterbox?
[perldigious]: If I was given the option to remove either mosquitos or ticks from the world... I think I'd pick ticks, even though I'd guess most would go the other way.
[Lotus1]: when they grooms themselves they eat any ticks they find. and apparently lots of ticks latch onto opossums.
[Your Mother]: There is a nice looking—don’t know first hand—new tool being market for removing ticks.
[Your Mother]: (marketed)
[Lotus1]: at least ticks don't fly
[1nickt]: We have a new tick in cenral New Jersey this year. The Lone Star tick -- no Lyme, but will give you an allergy to red meat!
[Your Mother]: "Tick Twister."
[1nickt]: My sister lives in the Sydney suburbs (carved out of the bush) and they have the Paralysis tick -- leavs you quadriplegic!
[perldigious]: Hmm, thanks Lotus1... perldigious scribbles note to buy lots of opossums as "pets" in the future to hang around outside his property. :-P
[1nickt]: You must twist and pull! If you burn, use tea tree oil etc, the tick barfs (inside your blood vessel) and you get all the loevely bacteria in his gut transferred to yours.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2017-05-24 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?