Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Regex Question: how to translate only the leading letters of a line

Reply
Thread Tools

Perl Regex Question: how to translate only the leading letters of a line

 
 
Yu
Guest
Posts: n/a
 
      07-10-2004
Hi,
I wonder if there is an elegant way of converting
number 1-9 into letter A-I for the LEADING letter
of a line. For example:

Input:
1 xxxx1234....

Ouput:
A xxxx1234....

The tr operator does not take the special position
character such as '^' and '$', so it would operate
on every match in the input line. Any input will
greatly appreciated.

-Yu
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-10-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Yu) wrote in news:75b30916.0407101452.37cad795
@posting.google.com:

> Hi,
> I wonder if there is an elegant way of converting
> number 1-9 into letter A-I for the LEADING letter
> of a line. For example:
>
> Input:
> 1 xxxx1234....
>
> Ouput:
> A xxxx1234....
>
> The tr operator does not take the special position
> character such as '^' and '$', so it would operate
> on every match in the input line.


What is preventing you from using s/// then?

#! perl

use strict;
use warnings;

my @repl = ('A' .. 'I');

while(<DATA>) {
s/^([1-9]) /$repl[$1-1] /;
print;
}

__DATA__
1 xxxx1234....
2 xxxx1234....
3 xxxx1234....
4 xxxx1234....
5 xxxx1234....
6 xxxx1234....
1other kind of line
7 xxxx1234....
8 xxxx1234....
9 xxxx1234....
0 xxxx1234....
0 xxxx1234....
3 xxxx1234....
5 xxxx1234....
6 xxxx1234....
7 xxxx1234....

--
A. Sinan Unur
(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-10-2004
Yu wrote:
> I wonder if there is an elegant way of converting number 1-9 into
> letter A-I for the LEADING letter of a line. For example:
>
> Input:
> 1 xxxx1234....
>
> Ouput:
> A xxxx1234....
>
> The tr operator does not take the special position character such
> as '^' and '$', so it would operate on every match in the input
> line.


substr($line, 0, 1) =~ tr/1-9/A-I/;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-10-2004
Purl Gurl <(E-Mail Removed)> wrote in news:40F07E19.AA9DE522
@purlgurl.net:

> Gunnar Hjalmarsson wrote:
>
>> Yu wrote:

>
>> > I wonder if there is an elegant way of converting number 1-9 into
>> > letter A-I for the LEADING letter of a line. For example:

>
>> > The tr operator does not take the special position character such

>
>
>> substr($line, 0, 1) =~ tr/1-9/A-I/;


....

> Interesting hack, yes? I am fascinated by how tr keeps
> track of numerical position for the right hand side.


tr does not keep track of anything. substr produces an lvalue.

Based on the OP's example, I concluded that the requirement was to
replace a digit followed by a space with the corresponding letter
followed by a space at the beginning of the line. Therefore, I did not
suggest the tr solution.


--
A. Sinan Unur
(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      07-10-2004
(E-Mail Removed) (Yu) wrote:

: I wonder if there is an elegant way of converting
: number 1-9 into letter A-I for the LEADING letter
: of a line. For example:
:
: Input:
: 1 xxxx1234....
:
: Ouput:
: A xxxx1234....

$_ ^= 'p';

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-10-2004
Purl Gurl wrote:
> Gunnar Hjalmarsson wrote:
>> Yu wrote:
>>> I wonder if there is an elegant way of converting number 1-9
>>> into letter A-I for the LEADING letter of a line. For example:

>>
>> substr($line, 0, 1) =~ tr/1-9/A-I/;

>
> Interesting hack, yes? I am fascinated by how tr keeps track of
> numerical position for the right hand side.
>
> However, I have been told many times using Perl hacks is bad, bad,
> BAD!


It's not a hack. According to "perldoc -f substr", substr() can be
used as an lvalue, so it's documented behaviour.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      07-11-2004
Purl Gurl <(E-Mail Removed)> wrote:

: Purl Gurl wrote:
:
: > Gunnar Hjalmarsson wrote:
: > > Yu wrote:
:
: > > > The tr operator does not take the special position character such
:
: > > substr($line, 0, 1) =~ tr/1-9/A-I/;
:
: > Copy cat. * meows *
:
: > Interesting hack, yes? I am fascinated by how tr keeps
: > track of numerical position for the right hand side.
:
:
: Here is another variation which is most interesting,
:
: $string = "abcdefghi123456789";
:
: $string =~ tr/1-9/A-I/;
:
: print $string;
:
: --
:
: abcdefghiABCDEFGHI

[truncated]

What intersting thing, other than the documented behavior of the tr///
operator, were these samples meant to demonstrate?

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-11-2004
Jay Tilton wrote:
>
> $_ ^= 'p';


Hmm ... I know what $_ is. Would you mind explaining ^= and 'p'? Are
they explained in the Perl docs, or are they C things that happen to
work in Perl as well?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-11-2004
Purl Gurl <(E-Mail Removed)> wrote in news:40F08B20.40522494
@purlgurl.net:

> Gunnar Hjalmarsson wrote:
>
>> Purl Gurl wrote:
>> > Gunnar Hjalmarsson wrote:
>> >> Yu wrote:

>
>> >> substr($line, 0, 1) =~ tr/1-9/A-I/;

>
>> > Interesting hack, yes? I am fascinated by how tr keeps track of
>> > numerical position for the right hand side.

>
>> It's not a hack. According to "perldoc -f substr", substr() can be
>> used as an lvalue, so it's documented behaviour.

>
> I am referencing the behavior of transliteration which
> behaves in a way which is not intuitive. You would not
> expect numbers to be translated to letters according to
> numerical value.


That is not what is happening. You are confused.

> You would expect numbers to be translated according to
> sequential position.


See:

#! perl

use strict;
use warnings;

while(<DATA>) {
substr($_, 0, 1) =~ tr/324568791/A-I/;
print;
}

__DATA__
1 xxxx1234....
2 xxxx1234....
3 xxxx1234....
4 xxxx1234....
5 xxxx1234....
6 xxxx1234....
7 xxxx1234....
8 xxxx1234....
9 xxxx1234....

C:\Develop\perl> t
I xxxx1234....
B xxxx1234....
A xxxx1234....
C xxxx1234....
D xxxx1234....
E xxxx1234....
G xxxx1234....
F xxxx1234....
H xxxx1234....


--
A. Sinan Unur
(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-11-2004
Purl Gurl wrote:
> Gunnar Hjalmarsson wrote:
>> Purl Gurl wrote:
>>> Gunnar Hjalmarsson wrote:
>>>>
>>>> substr($line, 0, 1) =~ tr/1-9/A-I/;
>>>
>>> Interesting hack, yes? I am fascinated by how tr keeps track of
>>> numerical position for the right hand side.

>>
>> It's not a hack. According to "perldoc -f substr", substr() can
>> be used as an lvalue, so it's documented behaviour.

>
> I am referencing the behavior of transliteration which behaves in a
> way which is not intuitive. You would not expect numbers to be
> translated to letters according to numerical value. You would
> expect numbers to be translated according to sequential position.


I'm not following you. tr/// translates characters irrespective of
their numerical values.

$_ = "8uesti65i5g\n";
tr/5-8/n-q/;
print;

Outputs:
questioning

> You have an Einstien quote on your homepage. Review it.


Do you mean:

"The important thing is never to stop questioning."

I agree that's wise, but please don't overdo it.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
making all letters Caps/Small Letters Merrigan Python 4 12-14-2007 10:10 AM
All leading tabs or all leading spaces - why isn't that enforced? John Nagle Python 4 08-07-2007 04:05 PM
RE: All leading tabs or all leading spaces - why isn't that enforced? Delaney, Timothy (Tim) Python 0 08-07-2007 03:50 AM
Changing from capital letters to small letters using perl Venugopal Perl Misc 11 11-05-2003 06:07 PM



Advertisments