How to find out if a string is in uppercase only

L

leifwessman

Hi!

How do I find out if a string is uppercase only? I've searched cpan for
a suitable module, but found nothing.

This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

Thanks for any input on how to create such a method.
Leif

Note: ÅÄÖ is swedish characters.
 
A

Anno Siegel

Hi!

How do I find out if a string is uppercase only? I've searched cpan for
a suitable module, but found nothing.

This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

Thanks for any input on how to create such a method.
Leif

Note: ÅÄÖ is swedish characters.

Check if any lower-case characters appear in the string:

my $lower = join '' => 'a' .. 'z', qw( å ä ö);
sub is_uppercase {
return !/[$lower]/ for shift;
}

If you have a working locale for your character set, you don't have
to set up the alphabet manually:

sub is_uppercase {
use Locale;
return !/[[:lower:]]/ for shift;
}

Anno
 
B

Brian McCauley

Hi!

How do I find out if a string is uppercase only?

I assume you really mean no lowercase since you example string also
contains a space.
I've searched cpan for
a suitable module, but found nothing.

No need, it's builtin.
This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

!/[[:lower:]]/

or

$_ eq uc
Note: ÅÄÖ is swedish characters.

That should be OK.
 
T

Tassilo v. Parseval

Also sprach Brian McCauley:
Hi!

How do I find out if a string is uppercase only?

I assume you really mean no lowercase since you example string also
contains a space.
I've searched cpan for
a suitable module, but found nothing.

No need, it's builtin.
This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

!/[[:lower:]]/

or

$_ eq uc
Note: ÅÄÖ is swedish characters.

That should be OK.

Only when 'use locale' is in effect. Otherwise

$_ = "ö";
if ($_ eq uc) {
...
}

reports as true.

Tassilo
 
B

Brian McCauley

Tassilo said:
Also sprach Brian McCauley:



Only when 'use locale' is in effect.

Yeah, sorry I realised that just after I hit send. But as I said the
other day - on Usenet there's no need to correct yourself, there are
thousands of people just waiting to leap at the chace to correct you :)
 
B

Bart Van der Donck

How do I find out if a string is uppercase only? I've searched cpan for
a suitable module, but found nothing.
This is what I'm looking for:
is uppercase("UPPERCASE ") # TRUE
is uppercase("UPPERCaSE ") # FALSE

I believe this should do the trick:

----------------------------------------------------
#!/usr/bin/perl
$isUppercase="OK";
$string="PUTSOMETESTSTRINGHERE";
@splitit = split //, $string;
for (@splitit) {
$isUppercase = "not OK"
if ($_!~/(A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z)/)
}
print "Upper case: $isUppercase";
----------------------------------------------------

You can extend the (A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z)
part to include the Swedish characters that you want to allow. (or if
you want spaces as in your example, etc)

hope this helps
Bart
 
J

John W. Krahn

How do I find out if a string is uppercase only? I've searched cpan for
a suitable module, but found nothing.

This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

Thanks for any input on how to create such a method.
Leif

Note: ÅÄÖ is swedish characters.

sub is_uppercase { require locale; $_[0] eq uc $_[0] }


John
 
J

Jürgen Exner

Hi!

How do I find out if a string is uppercase only? I've searched cpan
for a suitable module, but found nothing.

This is what I'm looking for:

is_uppercase("UPPERCASE ÅÄÖ") # TRUE
is_uppercase("UPPERCaSE ÅÄÖ") # FALSE

Thanks for any input on how to create such a method.
Leif

Note: ÅÄÖ is swedish characters.

Just convert the string to all upper case and then check if the original
string and the all upper string are equal.

jue
 
U

Uri Guttman

BVdD> if ($_!~/(A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z)/)

BLARGGGHH!!

learn about char classes please. that is so slow and hard to
maintain.

uri
 
J

Jürgen Exner

Bart said:
You can extend the
(A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) part to include
the Swedish characters that you want to allow. (or if you want spaces
as in your example, etc)

Arrrg, if a developer would present this as a global solution to me I would
have a serious talk with his manager the same day!

This doesn't scale well to other languages because it requires detailed
knowlegde of the other language (or do you know which extended characters to
add for Spanish, Italian, or Turkish).
It doesn't scale well to other character sets (what are the capital letters
for cyrillic Serbian)? Do you as an English-speaking programmer know how to
enter them on your keyboard?
And it doesn't scale to additional language because it requires a code
change whenever you add a new language/character set.

Bad, bad, bad.

jue
 
G

Gunnar Hjalmarsson

Jürgen Exner said:
This doesn't scale well to other languages because it requires
detailed knowlegde of the other language (or do you know which
extended characters to add for Spanish, Italian, or Turkish).

There is a separate charset for Turkish: ISO-8859-9 (which of course
emphasizes your point).
 
A

Alan J. Flavell

There is a separate charset for Turkish: ISO-8859-9 (which of course
emphasizes your point).

The properly engineered way to do this in present-day Perl is to read
the characters into Perl's native wide-character storage format, and
then use the Unicode character property function(s).

However, the original question seemed to be based on the premise that
a white space character was upper-case, whereas from Unicode's point
of view it's not. And at least one of the answers was based on the
assumption that all characters which didn't exist in a lower-case
version were defined to be upper-case, which again isn't Unicode's
view of the world. The Original Poster will need to take a decision
on this issue.

The character properties are listed below here:
http://www.perldoc.com/perl5.8.4/pod/perlunicode.html#Effects-of-Character-Semantics

The Perl unicode documentation warns not to mix unicode with locales.
(So if there's any character which is considered uppercase in one
locale and not uppercase in another...?)
 
J

Jürgen Exner

Gunnar said:
There is a separate charset for Turkish: ISO-8859-9 (which of course
emphasizes your point).

There is even a separate charset for German: ISO-646-DE
Just nobody is using it which probably is a good thing ;-)

jue
 
J

Jürgen Exner

Alan said:
However, the original question seemed to be based on the premise that
a white space character was upper-case, whereas from Unicode's point
of view it's not. And at least one of the answers was based on the
assumption that all characters which didn't exist in a lower-case
version were defined to be upper-case, which again isn't Unicode's
view of the world.

Climbing out on a limb here, but there is a chance that the OP actually
wanted to exclude lowercase characters rather then limit the string to upper
case only (although that's what he wrote).
The Original Poster will need to take a decision
on this issue.

Yep, quite true.

jue
 
B

Bart Van der Donck

Jürgen Exner said:
Arrrg, if a developer would present this as a global solution to me I would
have a serious talk with his manager the same day!

What makes you think I posted this as a global solution?? Of course
it's not. It is a piece of code that solves the problem he described.
This doesn't scale well to other languages because it requires detailed
knowlegde of the other language (or do you know which extended characters to
add for Spanish, Italian, or Turkish).
It doesn't scale well to other character sets (what are the capital letters
for cyrillic Serbian)? Do you as an English-speaking programmer know how to
enter them on your keyboard?
And it doesn't scale to additional language because it requires a code
change whenever you add a new language/character set.

All correct, but the original poster only needs Swedish characters as
he said.

Best regards,
Bart
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top