Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Need help with replace or substitute

by Anonymous Monk
on Apr 11, 2008 at 22:40 UTC ( [id://679879]=perlquestion: print w/replies, xml ) Need Help??

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

input string
$string='"the, sample, string",345,43';
output required
"the# sample# string",345,43
that means I have to replace commas within double quoted part of string to #

Replies are listed 'Best First'.
Re: Need help with replace or substitute
by grep (Monsignor) on Apr 11, 2008 at 22:53 UTC
    Actually, I see your problem right away.
    You have no code.

    perl needs code to run before it will do anything useful. Which is funny, because most perlmonks want you to write some code before they'll do anything useful.

    Take a look at perlre then write some code. Then ask a question.

    grep
    One dead unjugged rabbit fish later...
Re: Need help with replace or substitute
by Anonymous Monk on Apr 11, 2008 at 23:19 UTC
    as someone asked for code this is code snip... as whole code is quite big ..
    #!c:/perl/bin/perl $string='"the, sample, string",345,43","A, next , string",12,-90'; print "$string\n"; #######$string=~ s/<need help here>/<need help here>/g ; print "$string\n";
    output required:
    "the, sample, string",345,43","A, next , string",12,-90 "the# sample# string",345,43","A# next # string",12,-90'
      You might want to look at Text::xSV or Text::CSV or Text::CSV_XS to break up the fields. Then you can just s/,/#/g on each field, and print back out the results delimited by commas.

        Or with one line of regex code:

        $string =~ s/("[^"]*")/local $_=$1; s!,!#!g; $_ /ge;

        Yes it won't handle escaped double quotes but if you don't have that edge case it will never matter.

        Hey Thanks, it worked for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://679879]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found