How to find out if a string is in uppercase only

Discussion in 'Perl Misc' started by leifwessman@hotmail.com, Aug 28, 2004.

  1. Guest

    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.
    , Aug 28, 2004
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> 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
    Anno Siegel, Aug 28, 2004
    #2
    1. Advertising

  3. 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.
    Brian McCauley, Aug 28, 2004
    #3
  4. Also sprach Brian McCauley:

    > 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Aug 28, 2004
    #4
  5. Tassilo v. Parseval wrote:

    > Also sprach Brian McCauley:
    >
    >> 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 :)
    Brian McCauley, Aug 28, 2004
    #5
  6. 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
    Bart Van der Donck, Aug 28, 2004
    #6
  7. 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
    John W. Krahn, Aug 28, 2004
    #7
  8. 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
    Jürgen Exner, Aug 28, 2004
    #8
  9. Uri Guttman Guest

    >>>>> "BVdD" == Bart Van der Donck <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 28, 2004
    #9
  10. 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
    Jürgen Exner, Aug 28, 2004
    #10
  11. Jürgen Exner wrote:
    > 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).

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 28, 2004
    #11
  12. On Sat, 28 Aug 2004, Gunnar Hjalmarsson wrote:

    > Jürgen Exner wrote:
    > > 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).


    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...?)
    Alan J. Flavell, Aug 28, 2004
    #12
  13. Gunnar Hjalmarsson wrote:
    > Jürgen Exner wrote:
    >> 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).


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

    jue
    Jürgen Exner, Aug 28, 2004
    #13
  14. Alan J. Flavell wrote:
    > 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
    Jürgen Exner, Aug 28, 2004
    #14
  15. Jürgen Exner wrote:

    > 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
    Bart Van der Donck, Aug 29, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sudeep Batra - Bond 007

    Converting string to uppercase using Perl5Substitution

    Sudeep Batra - Bond 007, Apr 7, 2004, in forum: Java
    Replies:
    2
    Views:
    1,648
    Andrew Thompson
    Apr 8, 2004
  2. natG
    Replies:
    4
    Views:
    9,654
  3. shapper
    Replies:
    3
    Views:
    680
    George Ter-Saakov
    Nov 1, 2006
  4. The_Kingpin
    Replies:
    1
    Views:
    434
  5. Michal
    Replies:
    57
    Views:
    21,083
    Ian Collins
    Dec 24, 2008
Loading...

Share This Page