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

Replace Character Symbols with White Space

by MiriamH (Novice)
on Jun 25, 2012 at 16:04 UTC ( #978217=perlquestion: print w/replies, xml ) Need Help??
MiriamH has asked for the wisdom of the Perl Monks concerning the following question:

I need to clean up my data, but I do not know how to. I want to remove all the + signs, I realise that is probably simple, but I couldnt figure out how to replace a sybmbol with whitespace!

This is an example of the data I need to clean :

<Alan+Braxe+at+The+Hoxton+on+6+July+2012> <0000 2012-07-06T22:00:00Z 2012-07-06T23:59:59Z> <The+Hoxton> <Pop+Kult!+%23009> <0000 2012-07-06T22:00:00Z 2012-07-06T23:59:59Z> <Gladstone+Hotel> <Dimitri+Vegas+-+Like+Mike+at+Maison+Mercer+on+6+July+2012> <0000 2012-07-06T22:00:00Z 2012-07-06T23:59:59Z> <Maison+Mercer> <Aesthetic+Perfection+at+Neu-ral+on+7+July+2012> <0000 2012-07-07T00:00:00Z 2012-07-07T23:59:59Z> <Neu%252Bral> <K.Flay+at+Wrongbar+on+7+July+2012> <0000 2012-07-07T19:00:00Z 2012-07-07T23:59:59Z>

  • Comment on Replace Character Symbols with White Space

Replies are listed 'Best First'.
Re: Replace Character Symbols with White Space
by davido (Archbishop) on Jun 25, 2012 at 16:09 UTC

    The tr/// operator (perlop).

    $string =~ tr/+/ /;

    Example:

    perl -e '$string = "+=+=+=+"; $string =~ tr/+/ /; print "($string)\n"; +'

    ...the output...

    ( = = = )

    If it's possible that your data has legitimate '+' characters (perhaps escaped, or quoted), then you'll have to use a more elaborate solution.

    I think that perlintro is required reading for anyone new to Perl, and while the tr/// operator isn't discussed therein, the s/// is. The s/// operator could be used (less efficiently than tr///) like this: s/[+]/ /g. So after reading through perlintro, while you might not have discovered the most efficient way of doing it, you would have gotten a good start toward coming up with a reasonable way of doing it.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://978217]
Approved by davido
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-04-25 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?