Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Answer: How do I put bracket on substring(s) of a string?

by jdporter (Canon)
on Sep 10, 2008 at 19:55 UTC ( #710453=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I put bracket on substring(s) of a string? - Answer contributed by jdporter

sub put_brackets { my( $str, @pats ) = @_; my $mask = ' ' x length $str; my $ofs = 0; for ( @pats ) { $ofs = index $str, $_, $ofs; last if $ofs < 0; # this pattern not found; look for no more. substr $mask, $ofs, length($_), 'a' x length($_); } # print "string= '$str'\n"; # print "mask = '$mask'\n"; join '', map sprintf( /a/ ? '[%s]' : '%s', substr $str, 0, length($_), '' ), split /\b/, $mask }

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-11-20 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (282 votes). Check out past polls.

    Notices?