Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to remove double quotes?

by basicdez (Pilgrim)
on Jun 05, 2001 at 20:01 UTC ( [id://85859]=perlquestion: print w/replies, xml ) Need Help??

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

I am taking a line of text from an xml tree and passing it through perl into software that reads the double quotes as end of line characters. how do I use perl to strip out the " characters before passing the data string?

Replies are listed 'Best First'.
Re: How to remove double quotes?
by davorg (Chancellor) on Jun 05, 2001 at 20:06 UTC

      it occurred to me that if the double-quotes are at all valuable in and of themselves, and happen to just be a problem for the recieving program, you might want to consider translating them to something other than an empty string, i.e.:

      $string =~ tr/"/'/d;

      of course the problem here being that you run the risk of changing the meaning of the string in other ways

      just my $.02
        Following on from this point.
        Check the docs on the software which is treating the " as a line ending character. It might have an escape syntax for the " and other characters it has special behaviours for.
Re: How to remove double quotes?
by bikeNomad (Priest) on Jun 05, 2001 at 20:44 UTC
    Your description is a bit vague. I assume that you have a perl variable that happens to have double quote characters inside it, though why that would be needed in XML is a bit beyond me (XML is its own delimiter, and shouldn't need extra ones).

    Removing double quotes from around a Perl string is pretty easy:

    $string =~ s/^"(.*)"$/$1/; # or (faster if you always have quotes): $string = substr($string, 1, length($string)-2);
    Though this doesn't answer the question about what to do with embedded quotes.

    Anyway, you are sending this string to some other software somehow.

    Here's where it gets difficult. There are several ways to send data to other programs:

    • through the system() call
    • using qx() or backticks
    • through an API like OLE
    • via temporary files
    • through data in a pipe
    • through data over a socket or datagram connection
    The solution for each of these is likely to be different.

    If you're using system() or qx(), you have to watch out for the quoting behavior of your local shell (which might view double quotes as special).

    More info is needed...

Re: How to remove double quotes?
by andreychek (Parson) on Jun 05, 2001 at 20:50 UTC
    If I'm correct in what it is you are looking for, you could just do this:
    my $string = qq{"Hello there"}; $string =~ s/"/'/g;
    That uses a simple regular expression to change every double quote to a single quote.
    You could, instead of a single quote, replace it with any character you like. HTH
    -Eric

    Update
    Ack, apparently, you have to type pretty fast or else 5 people get their answers in before you do ;-)
    Would anyone care to share why perl can do tr/"/'/ faster than s/"/'/?
        Would anyone care to share why perl can do tr/"/'/ faster than s/"/'/?

      Because s/// has to be able to handle all RE syntax and specialness while tr/// just considers its input to (mostly) be a simple character class. Much easier to deal with and, thus, much faster.

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found