#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    8
    Rep Power
    0

    Replace Degree Symbol


    I've looked this up for a few days now and can't get it.
    I'm on a Mac. I have a text file with the degree symbol in it (on Mac: Opt+Shift+8). I want my perl script to replace all degree symbol's with a period. I can't figure it out.

    I know if I set:
    binmode(STDOUT, ":utf8");
    then,
    print chr(0x00b0);
    I will get a degree sign printed to the terminal.
    But whenever I try to search for it, like:
    perl -pi -e 's/\x{00B0}/\./g' txtfile
    It doesn't work. I've tried other variations too.
    Can someone help me with this symbol. I know that if I open the text file up on a Windows box, it's an inverted exclamation mark, if that helps.
    Thanks in advance.
    Eric
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    142
    Rep Power
    31
    Hi, maybe you need to get the exact hex code for the "degree" represented in your textfile. Maybe the following link can help :
    http://www.dev411.com/blog/2006/09/29/perl-getting-a-unicode-characters-hex-codepoint
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    8
    Rep Power
    0

    exact hex code for the "degree" symbol


    Originally Posted by joej0e
    Hi, maybe you need to get the exact hex code for the "degree" represented in your textfile. Maybe the following link can help :
    (URL address blocked: See forum rules)
    joejOe,
    I've tried the exact hex code for the "degree" symbol in my textfile, b0, but I'm still having trouble. Thanks.
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,263
    Rep Power
    1810
    From a Mac:
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use utf8;
    
    my $string = '';
    print ord $string;
    Don't know if it will display on the web page, but my string is Option-Shift-8, and displays as a degree character in my text editor.

    Produces the output: 176
    Decimal 176

    That is the unicode value for a degree sign, and I specified utf8 as the encoding. Without that, you'd get a different result.

    What Mac version are you using? What program is displaying the character as a degree sign?

    Latin-1 for the inverted exclamation point is decimal 161.

    You can always take one of your strings from the file and print out that character using 'ord' to see it's real value. Without knowing the file's encoding you'd just be guessing otherwise.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Location
    Manchester/England
    Posts
    34
    Rep Power
    15
    Most of the time the trick in these situation, is to write the part of the script that is giving you trouble (with me it was the English pound symbol), in the Perl editor itself instead of a normal text editor. The symbols you type in the Perl editor come out exactly as saved.

    Hope this helps.
    Stu
  10. #6
  11. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,263
    Rep Power
    1810
    Yeah, that works sometimes, but in my script above, if I comment out the 'use utf8' line I get 194. If I don't I get 176.

    If you are going to type in literals, you have to use the same encoding as the file you want to match; and in this case, he doesn't know what the encoding is (at this point).

IMN logo majestic logo threadwatch logo seochat tools logo