Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: not able to replace the element in the middle of the string

by hippo (Bishop)
on Jul 12, 2016 at 08:40 UTC ( [id://1167599]=note: print w/replies, xml ) Need Help??


in reply to not able to replace the element in the middle of the string

TIMTOWTDI

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 3; my $have = 'aabbccddaaffddnnaa'; my $want = 'aabbccddeeffddnnaa'; my $aa = $have; # perldoc perlre $aa =~ s/daaf/deef/; is ($aa, $want, 'Match explicit surrounds'); # perldoc perlre $aa = $have; $aa =~ s/(.)aa(.)/$1ee$2/; is ($aa, $want, 'Match non-terminal surrounds'); # perldoc -f substr $aa = $have; substr ($aa, length($aa)/2 - 1, 2, 'ee'); is ($aa, $want, 'Replace middle 2 chars of even-length string');

Since you've given no algorithm to determine what to replace choose whichever suits.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found