Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Substituting a comma only when it is preceded and followed by a bracket

by JavaFan (Canon)
on Nov 02, 2011 at 22:52 UTC ( #935516=note: print w/ replies, xml ) Need Help??


in reply to Substituting a comma only when it is preceded and followed by a bracket

Untested:

my $str = do {my $i; join "", map {$_ eq '(' ? $i++ : $_ eq ')' && $i +? $i-- : $_ eq ',' && $i ? ($_ = ';') : 0; $_} split '[(,)]', $str};
This assumes the parenthesis are balanced.

But what should happen on:

"(foo),(bar)"
? The comma here is preceded and followed by a bracket - preceded by an opening paren, and followed by a closing paren even. If such a comma should be replaced as well, one might use something like (untested):
substr($str, index($str, "("), rindex($str, ")") - length($str)) =~ s/ +,/;/g;


Comment on Re: Substituting a comma only when it is preceded and followed by a bracket
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2015-07-03 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls