Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to replace a matched pattern in a string with a different pattern of same length?

by wind (Priest)
on Jun 20, 2011 at 22:18 UTC ( #910633=note: print w/ replies, xml ) Need Help??


in reply to how to replace a matched pattern in a string with a different pattern of same length?

First calculate the longest using List::Util->max and then do a substitution.

use List::Util qw(max); use strict; use warnings; my $string = '00111100110'; my $longest = max map {length} $string =~ /(1+)/g; $string =~ s/1{$longest}/'2' x $longest/eg; print $string, "\n";


Comment on Re: how to replace a matched pattern in a string with a different pattern of same length?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-08-30 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls