Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Replacing non ascii in string

by naChoZ (Curate)
on Jan 30, 2013 at 19:15 UTC ( #1016138=note: print w/replies, xml ) Need Help??


in reply to Replacing non ascii in string

Hardly an elegant solution, but my little script will do exactly what you want.

#!/usr/bin/perl -n #use strict; #use warnings; use charnames (); use encoding "utf8"; $|++; my $chars = { 'HYPHEN' => '-', # \x{2010} 'SOFT HYPHEN' => '-', # \x{00AD} 'MINUS SIGN' => '-', # \x{2212} 'FIGURE DASH' => '-', # \x{2012} 'ACUTE ACCENT' => "'", # \x{00B4} 'GRAVE ACCENT' => "'", # \x{0060} 'LEFT SINGLE QUOTATION MARK' => "'", # \x{2018} 'RIGHT SINGLE QUOTATION MARK' => "'", # \x{2019} 'LEFT DOUBLE QUOTATION MARK' => '"', # \x{201C} 'RIGHT DOUBLE QUOTATION MARK' => '"', # \x{201D} 'BOX DRAWINGS LIGHT VERTICAL' => '|', # \x{2502} 'MULTIPLICATION SIGN' => '*', # \x{00D7} 'BACKSPACE' => '', # \x{0008} 'DELETE' => '', # \x{0127} }; # If the first character is an equal sign, skip it and # display the identity of each remaining character. # if (/^=/) { chomp; for my $index ( 1 .. length($_) - 1 ) { my $char = substr( $_, $index++, 1 ); print $char . " " . ord( $char ) . " " . sprintf( "\\%03o", ord($char) ) . " " . sprintf( "\\x{%04X}", ord($char) ) . " = '" . charnames::viacode( ord($char) ) . "'\n" ; } } else { for my $cname ( keys %$chars ) { my $char = chr( charnames::vianame($cname) ); s/$char/$chars->{$cname}/g; } print; }

Run this and it sits there waiting for you to paste something to your terminal. Once it receives some input, it starts replacing characters by their unicode name with the values from the $chars hashref.

If you need to identify any unicode characters because you want to add to the hashref of replacement characters, press the = key first and then paste to your terminal.

--
Andy

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016138]
help
Chatterbox?
[Discipulus]: new $workstation is arrived: SSD 250, sata 1TB, 16G RAM, only i5 CPU.. seems a good one: guess what first user installation will be?
[Corion]: Discipulus: Notepad++, or Perl ;)
[marto]: xubuntu or lubuntu
[Corion]: :-D
[marto]: my desktop is a core2 duo, 120GB SSD, 4GB RAM, boots to (GUI) login in less than 10 seconds from cold start
[Discipulus]: mah, winbuntu 10...
[marto]: whent he boys start school I'll build myself as new machine, ryzen based
[Corion]: marto: That's cool! I think my (home) desktop spends 30 seconds in the BIOS...
[Corion]: marto: Heh - currently they seem promising, but I think I'll stay with nVidia+Intel for the time being, as I've been bitten too often by bad AMD drivers
Discipulus is (still?) not a big fan of notepad++

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-07-27 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (407 votes). Check out past polls.