tr problem

I

IanW

If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how can I
do this with tr///?

Incidentally, "listname" will never have any spaces in it and will always be
at the beginning of the string, so I had thought that sth like this might
work, but it just makes the whole thing upper case:

$_ =~ tr/^a-z+(?=\s+)/A-Z/;

Ian
 
T

Tore Aursand

If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how can I
do this with tr///?

Incidentally, "listname" will never have any spaces in it and will always be
at the beginning of the string, so I had thought that sth like this might
work, but it just makes the whole thing upper case:

$_ =~ tr/^a-z+(?=\s+)/A-Z/;

What you really is saying, is that the _first word_ in the sentence should
be converted to uppercase?

s{^(.+?\s+)}{ uc($1) }e;
 
A

Anno Siegel

IanW said:
If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how can I
do this with tr///?

$_ = "listname A short description of the list";
substr( $_, 0, index $_, ' ') =~ tr/a-z/A-Z/;

But see below.
Incidentally, "listname" will never have any spaces in it and will always be
at the beginning of the string, so I had thought that sth like this might
work, but it just makes the whole thing upper case:

$_ =~ tr/^a-z+(?=\s+)/A-Z/;

tr/// works on plain strings (whose characters determine what is
changed), not on regular expressions. Also, it isn't the best tool
to change case -- Perl's built-in functions lc and uc do a better job
of it, as they respect locale. Use a regular expression to do that.

Anno
 
J

John W. Krahn

IanW said:
If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how can I
do this with tr///?

Incidentally, "listname" will never have any spaces in it and will always be
at the beginning of the string, so I had thought that sth like this might
work, but it just makes the whole thing upper case:

$_ =~ tr/^a-z+(?=\s+)/A-Z/;

tr/// translates the characters on the left with the corresponding characters
on the right so '^' will be replaced with 'A', 'a' with 'B', 'b' with 'C' and
so on up to the characters 'z', '+', '(', '?', '=', 's', '+' and ')' which
will all be replaced with 'Z'. You need to use the substitution operator instead.

s/^(a-z+)(?=\s)/\U$1/;


John
 
I

IanW

[snip]
tr/// works on plain strings (whose characters determine what is
changed), not on regular expressions. Also, it isn't the best tool
to change case -- Perl's built-in functions lc and uc do a better job
of it, as they respect locale. Use a regular expression to do that.

Yes, I see what you mean, from Tore's reply... I've just looked up the "e"
modifier on regexps - I didn't realise you could include functions like uc
in a regexp.. cool :)

Thanks guys
 
I

IanW

John W. Krahn said:
tr/// translates the characters on the left with the corresponding characters
on the right so '^' will be replaced with 'A', 'a' with 'B', 'b' with 'C' and
so on up to the characters 'z', '+', '(', '?', '=', 's', '+' and ')' which
will all be replaced with 'Z'. You need to use the substitution operator instead.

s/^(a-z+)(?=\s)/\U$1/;

\U is another new one one me! I need a new reference book ;-)

Btw, I could shorten that regexp more by doing:

$_ =~ s/^(\S+)/\U$1/;

Thanks
 
J

Joe Smith

IanW said:
If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how can I
do this with tr///?

You can't. If you ask tr/// to change one 'l' to 'L', it will do so for all.
Incidentally, "listname" will never have any spaces in it and will always be
at the beginning of the string,

'beginning of the string' == 'time to use a regex, such as s///, not tr///.'

s/^(\w*)/\U$1/; # If words do not include punctuation
s/^(\S*)/\U$1/; # If they do

-Joe
 
T

Tad McClellan

IanW said:
\U is another new one one me! I need a new reference book ;-)


No new book is required.

Just read the "Quote and Quote-like Operators" section in
the documentation for the programming language that you are using:

perldoc perlop


Btw, I could shorten that regexp more by doing:

$_ =~ s/^(\S+)/\U$1/;


If short is your goal, then you can improve it even further:

s/^(\S+)/\U$1/;
 
J

Jürgen Exner

IanW said:
If I have a string like this

"listname A short description of the list"

and I want to make the "listname" part of the string upper case, how
can I do this with tr///?

Why do you want to use tr?
Most people would probably choose uc() instead, in particular because uc()
will work worldwide while coding the correct character lists for non-English
charactes for tr must be a major endevour.
Incidentally, "listname" will never have any spaces in it and will
always be at the beginning of the string, so I had thought that sth
like this might work, but it just makes the whole thing upper case:

$_ =~ tr/^a-z+(?=\s+)/A-Z/;

Where did you get the idea that tr would use REs?

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top