Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to insert a "/" within a string?

by trizen (Friar)
on Feb 21, 2012 at 12:14 UTC ( #955294=note: print w/ replies, xml ) Need Help??


in reply to How to insert a "/" within a string?

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


Comment on Re: How to insert a "/" within a string?
Download Code
Re^2: How to insert a "/" within a string?
by rovf (Priest) on Feb 21, 2012 at 13:11 UTC
    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>
Re^2: How to insert a "/" within a string?
by GrandFather (Cardinal) on Feb 21, 2012 at 19:41 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955294]
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: (4)
As of 2014-10-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (141 votes), past polls