Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 cooling their heels in the Monastery: (16)
As of 2014-09-30 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (373 votes), past polls