LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2010, 04:37 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
Perl: Test if string is uppercase


Hi.


In my perl script I'd like to test if a string is written in uppercase letters or not. How can I do that? This type of test don't seem to work, so there must be other ways of doing this:

Code:
if ("hello" == "HELLO") { .... }
...return true.

I can create a subroutine that compares each character aginst a list of uppercase letters, but I'm hoping there's allready a build in routine in perl that does this...

Any ideas?

- kenneho
 
Old 01-06-2010, 04:43 AM   #2
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
I am new to perl but I think we can get that using the following little snippet.
Code:
$a="HELLO";
if ( $a eq "\U$a" )
{
print $a;
}

Last edited by ashok.g; 01-06-2010 at 06:38 AM. Reason: for more precise code
 
Old 01-06-2010, 05:35 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Thansk for your tip, but I didn't quite get it to work. This code:
Code:
#/usr/bin/perl 
$my_string = "hello"; 
if ($my_string =~ /[A-Z]*/) { 
    print "$my_string is uppercase.\n"; 
}
resultet in this output:
Code:
hello is uppercase.
Is it maybe necessary to loop through alle the characters, performing that same test?
 
Old 01-06-2010, 05:53 AM   #4
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
I caved in, and made use of this function I found somewhere on the net:
Code:
sub is_uppercase { 
    my ($string) = @_; 
    @splitit = split //, $string; 
    for (@splitit) { 
        # Checking each character if its uppercase...
        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)/) { 
            # We found a lowercase character! 
            return undef; 
        } 
    } 
    return 1; 
}
I find it strange if perl doesn't have built in functions for checking if a string is uppercase or not. :/
 
Old 01-06-2010, 06:17 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
I'd guess you are using a UTF locale - and hence collating sequence. Try it as
Code:
if ($my_string =~ "\U$my_string") {
 
Old 01-06-2010, 06:26 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Quote:
Originally Posted by kenneho View Post
I find it strange if perl doesn't have built in functions for checking if a string is uppercase or not. :/
It's not true . We have the functions in perl which can convert your string to lowercase as well as the upper case.

For Converting to LOWER case we have "lc". USAGE: lc "HELLO"
For Converting to UPPER case we have "uc". USAGE: uc "hello"
 
Old 01-06-2010, 06:38 AM   #7
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
I made some changes to my code and its now edited. Hope that will help you.
 
Old 01-06-2010, 07:38 AM   #8
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by ashok.g View Post
It's not true . We have the functions in perl which can convert your string to lowercase as well as the upper case.

For Converting to LOWER case we have "lc". USAGE: lc "HELLO"
For Converting to UPPER case we have "uc". USAGE: uc "hello"
Thanks for the tip, but I don't need to convert the string, I need to check if a string is uppercase...
 
Old 01-06-2010, 07:39 AM   #9
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by ashok.g View Post
I made some changes to my code and its now edited. Hope that will help you.
Cool, I'll give it a try!
 
Old 01-06-2010, 07:41 AM   #10
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by kenneho View Post
Cool, I'll give it a try!
I work! Great. "eq" instead of "==" was the solution. Thanks.
 
Old 01-06-2010, 03:31 PM   #11
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by kenneho View Post
Thanks for the tip, but I don't need to convert the string, I need to check if a string is uppercase...
Right, but think a bit more...
Code:
if ($string eq uc($string)) {
    # It's uppercase
}
That way, you don't need to know in advance what's stored in $string.
 
Old 01-07-2010, 01:50 AM   #12
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
To add up to all your solution, The following code should work for the problem.

Code:
my $str = "STRING\n";
if ($str !~ /[a-z]/)
{
        print "$str The string is uppercase\n";
}
else
{
        print "$str contains lowercase characters\n";
}
 
Old 01-07-2010, 03:55 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Doesn't pay to presume too much - does "upper/lower case" imply "ONLY upper/lower case" ?.
Stick a non alpha (say a digit) in the test string and run the tests above ...
 
Old 01-07-2010, 04:02 AM   #14
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Quote:
Originally Posted by syg00 View Post
Stick a non alpha (say a digit) in the test string and run the tests above ...
If that is the case then we have to go for checking each and every character with [A-Z]. Isn't it?
 
Old 01-07-2010, 08:47 AM   #15
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by Telemachos View Post
Right, but think a bit more...
Code:
if ($string eq uc($string)) {
    # It's uppercase
}
That way, you don't need to know in advance what's stored in $string.
Yeah, this is a more neat way of doing it (which I happened to have allready done
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Test for last character in string cgraham1 Programming 14 06-11-2009 01:25 PM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 05:57 PM
grep for string and test if successfully or not B-Boy Programming 3 02-17-2009 01:43 PM
ash test: is string A contained in string B chochem Programming 6 09-24-2008 06:59 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration