Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by lakshmikant (Initiate)
on Jul 12, 2016 at 05:08 UTC ( #1167593=perlquestion: print w/replies, xml ) Need Help??
lakshmikant has asked for the wisdom of the Perl Monks concerning the following question:

I have a string

$a = aabbccddaaffddnnaa
I am trying to replace the middle element of the string aa to ee but its not working, please let me know how to do that.
$a=~ s/^aa$/ee/

Replies are listed 'Best First'.
Re: not able to replace the element in the middle of the string
by Ratazong (Monsignor) on Jul 12, 2016 at 05:42 UTC
    $a=~ s/^aa$/ee/

    Do a bit research what ^ and $ mean in the context of a regular expression. Then it will become clear why your replacement won't work.

    Happy learning! Rata

    PS.: a good start is here: planetscapes collection

Re: not able to replace the element in the middle of the string
by hippo (Canon) on Jul 12, 2016 at 08:40 UTC

    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.

Re: not able to replace the element in the middle of the string
by Athanasius (Bishop) on Jul 12, 2016 at 06:06 UTC
Re: not able to replace the element in the middle of the string
by Laurent_R (Canon) on Jul 12, 2016 at 10:43 UTC
    You have to specify how to determine which "aa" sequence to replace. Assuming you want to replace any "aa" sequence that is neither at the beginning, not at the end, of the string, you may want to use a look-behind and a look-ahead assertion. This is an example under the Perl debugger:
    DB<1> $aa = "aabbccddaaffddnnaa"; DB<2> $aa =~ s/(?<=.)aa(?=.)/ee/; DB<3> print $aa aabbccddeeffddnnaa
    HTH.
Re: not able to replace the element in the middle of the string
by wrinkles (Pilgrim) on Jul 12, 2016 at 05:28 UTC
    First you should try to run your code with "use strict;".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-10-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (111 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!