Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to find out if a string is in uppercase only

Reply
Thread Tools

How to find out if a string is in uppercase only

 
 
leifwessman@hotmail.com
Guest
Posts: n/a
 
      08-28-2004

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.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-28-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> 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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-28-2004


(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      08-28-2004
Also sprach Brian McCauley:

> (E-Mail Removed) wrote:
>> 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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-28-2004
Tassilo v. Parseval wrote:

> Also sprach Brian McCauley:
>
>>(E-Mail Removed) wrote:
>>
>>>Note: is swedish characters.

>>
>>That should be OK.

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

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      08-28-2004
(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-28-2004
(E-Mail Removed) wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      08-28-2004
(E-Mail Removed) wrote:
> 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


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-28-2004
>>>>> "BVdD" == Bart Van der Donck <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Jrgen Exner
Guest
Posts: n/a
 
      08-28-2004
Bart Van der Donck wrote:
> 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


 
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
Make the first letter of a string uppercase shapper ASP .Net 3 11-01-2006 07:52 PM
Regex-ANT: Find LAST occurence of char, and uppercase it. natG Java 4 02-02-2005 03:48 PM
Function that standardize a string with lowercase and uppercase ? The_Kingpin C Programming 1 10-26-2004 06:31 AM
Converting string to uppercase using Perl5Substitution Sudeep Batra - Bond 007 Java 2 04-08-2004 10:52 AM



Advertisments