Many thanks for the various answers!

I had come across the tr option while searching for a solution yesterday, but 
can't use it because I only need to change certain letters to numbers in a few 
locations in a larger text file that also contains regular text (where I 
wouldn't want to change vowels into numbers ;).

Thomas


-----Original Message-----
From: John W. Krahn [mailto:jwkr...@shaw.ca] 
Sent: Thursday, January 03, 2013 9:19 AM
To: Perl Beginners
Subject: Re: Substituting letters for numbers

Hamann, T.D. wrote:
> Hello,

Hello,


> Given a string:
>
> i994
>
> where I want to replace the 'i' by a '1' the following regex 
> succesfully replaces the letter by a number:
>
> s/(i)(\d\d\d)/1$2/;

tr/i/1/;


> However, given a string:
>
> i99o
>
> where I want to replace the 'i' by a '1' and the 'o' by a '0' (zero), 
> the following regex fails:
>
> s/(i)(\d\d)(o)/1$20/;

tr/io/10/;


> for the obvious reason that perl looks for a pattern match at bracket 
> set #20, which doesn't exist.
>
> I can fix this by inserting a space in front of the zero, like this:
>
> s/(i)(\d\d)(o)/1$2 0/;
>
> and then using a second regular expression to remove the space, but 
> that somehow seems silly. Surely there is a quicker way to do this?

s/(i)(\d\d)(o)/1${2}0/;



John
--
Any intelligent fool can make things bigger and more complex... It takes a 
touch of genius - and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

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





--
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