Mike Flannigan wrote:

I want to change character code 160 to character
code 32 throughout a bunch of text files.  I'm using
this right now
s/(.)/ord($1) == '160' ? chr(32) : $1 /eg;

Here you are using the decimal numbers 160 and 32. This is very inefficient as you are searching for *every* character (except newline) and replacing every character. It would be much simpler and more efficient to just search for the character you want and replace only that character:

my $search  = quotemeta chr 160;
my $replace = chr 32;

s/$search/$replace/g;


and it works, but I don't like it much.  If anybody
has another way they like better, I'd appreciate
seeing it.  It does not have to be a reg exp.

Anybody know why this doesn't work?
tr/\160/\32/d;
Oddly it replaces 'p' (character code 80) with
character code 26???

The escape sequences you are using are octal (not decimal) representations of the ordinal values of the characters. You have to convert the decimal values 160 and 32 to the octal values 240 and 40.

$ perl -le'printf "%d decimal = %o octal and %d decimal = %o octal\n", 160, 160, 32, 32'
160 decimal = 240 octal and 32 decimal = 40 octal

So it then becomes:

tr/\240/\40/;

(You do not need the /d option as you are not deleting any characters.)

Or using substitution:

s/\240/\40/g;




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to