rsvp1920 has asked for the wisdom of the Perl Monks concerning the following question:

I want to write aprogram which will take the input file from user. Suppose the input file has a string ABCOPCOGNHVCOHL. The program should insert a "/" between every Cand O. I don't know that in file what will be the positions of C and O.

Replies are listed 'Best First'.
Re: How to insert a "/" within a string?
by moritz (Cardinal) on Feb 21, 2012 at 10:40 UTC
      This will also work...
      my $string = 'ACOCOB'; $string =~ s/CO/C\/O/g; print "$string\n";
        Yes, but he intentionally avoided using "/" as a delimiter to avoid "leaning toothpick syndrome".
Re: How to insert a "/" within a string?
by ambrus (Abbot) on Feb 21, 2012 at 11:25 UTC

    Just write it as two mutually recursive functions:

    $_ = "ABCOPCOGNHVCOHL"; my $P = sub { my($p, $q, $e, @s) = @_; $e, @s ? &{"C" ne $e ? $p : $q} +($p, $q, @s) : () }; my $Q = sub { my($p, $q, $e, @s) = @_; ("O" ne $e ? () : "/"), &$p($p, + $q, $e, @s) }; print join "", &$P($P, $Q, split //), "\n";
    (Don't take this answer seriously.)
      In fact, one should be inserting ⚡ instead of /, and it should go between C and D, not C and O:
      $string = 'ACDC';
Re: How to insert a "/" within a string?
by rovf (Priest) on Feb 21, 2012 at 10:43 UTC
    Use the g-modifier of the substitution operator. For example: If
    my $string = 'ACOCOB';
    then

    $string =~ s(CO)(C/O)g; print "$string\n";
    prints AC/OC/OB

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: How to insert a "/" within a string?
by trizen (Hermit) on Feb 21, 2012 at 12:14 UTC
    index + substr:
    my $str = 'ABCOPCOGNHVCOHL'; my $index = 0; while (($index = index($str, 'CO', $index)) > -1) { substr $str, $index, 2, 'C/O'; } print "$str\n"; __END__ ABC/OPC/OGNHVC/OHL
      To be in the same spirit as most of the other solutions proposed here, you should have written ($index = index($str, 'CO', $index)) > -1 as

      lc($index = index($str, 'CO', $index)) =~ /^-/
      ;-)

      -- 
      Ronald Fischer <ynnor@mm.st>

      Or using substr to insert a string between two characters:

      $_ = 0; substr $str, $_ + 1, 0, '/' while ($_ = index $str, 'CO', $_) > -1;
      True laziness is hard work
Re: How to insert a "/" within a string?
by oko1 (Deacon) on Feb 22, 2012 at 00:58 UTC

    Why can't you guys just give a nice straight answer, like this one?

    s/(?<O>\103)(?<C>\117)/$+{O}\057$+{C}/g;

    (This answer is just as serious as the ones before it, and the suggestions for appropriateness of usage still apply.)

    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"
Re: How to insert a "/" within a string?
by ikegami (Patriarch) on Feb 22, 2012 at 00:04 UTC

    In this case,

    s{CO}{C/O}g

    would do. If there's a chance of overlap between the two patterns to separate (e.g. putting "/" between "AC" and "CA"), you need a lookahead.

    s{AC(?=CA)}{AC/}g

    or

    s{AC\K(?=CA)}{/}g