bobothesmart has asked for the wisdom of the Perl Monks concerning the following question:

Here's my problem:
I have a string (say $str) which will contain either an 8 digit number or a 6 character string in the form:
number char number number number number
(such as: 2F5567, 4H6712, etc).

Now, there is the possibility that when a user enters their string, they enter: 2f5567, when I need it to be 2F5567 in order to do the requested operations on it. How do I:
1. Figure out if the second character is a letter of the alphabet?
2. Convert that character to its uppercase form?

Thanks

Replies are listed 'Best First'.
Re: Comparing lower to upper case
by Kanji (Parson) on Jun 11, 2002 at 18:21 UTC

    If you only need things to be the same case for the comparison, then you can use uc or lc to force your strings to be upper or lower.

    if ( uc($str) eq uc($something) ) { # ... }

    Alternately, and if you need care about the original case, you can reassign $str to an upper- or lowercased version of itself before you do anything else.

    $str = uc($str); # force uppercase

        --k.


      My statement looks like this:
      $fields[[0]] = uc($fields[[0]]);

      However, it does change this: 7f8954 into 7F8954.
      Id there a way to explicitely change just the one letter to uppercase?

      SORRY! It should say that it does NOT convert 7f8954 to 7F8954
        However, it does change this: 7f8954 into 7F8954.
        What? I thought that's what you wanted it to do...

        SORRY! It should say that it does NOT convert 7f8954 to 7F8954
        I'm confused now. You mean $fields[0] = uc($fields[0]); isn't converting 7f8954 to 7F8954?

        $fields[0] = "7f8954"; $fields[0] = uc($fields[0]);

        $fields[0] should now have a value of "7F8954"

        $fields[0] =~ tr/a-z/A-Z/; will also work.

        Joshua

Re: Comparing lower to upper case
by vladb (Vicar) on Jun 11, 2002 at 18:28 UTC
    Answering your first question:

    1. Figure out if the second character is a letter of the alphabet?

    You could simply do a pattern match, like so:
    if ($str =~ m/\d[a-zA-Z]\d+/) { print "Ok!\n"; } else { print "Invalid format! Must be: [digit][letter][digit][digit] ... \n +"; }


    UPDATE: pardon for this minor slip, but you are certainly right. It should have been [a-zA-Z] instead of just \w. ;/.

    _____________________
    open(I,$0);<I>;$~=$/;$/='~';$_=<I>;($/)=/(.)$~/;s/[$~~]//g;/(v)/;$-=$-[0];s;\Q$/\E;$~;g;($/,$^)=/^(.)(.)/;
    #%  xxxxxx   xx-+  xx    xxx xx  xx       xx  xx   xxx   xxxxx+ xx  xx xxxx xxxxx  ......+
    #x xxxxvxxx xx  xx xv   xxxx x+ %+  ===== xx  xx  xx xx  x+  =x xx xx  xx   xx xx ...+
    #x xx xx xx xx  xx xx  xx xx xxx+         xxxxxx xx   +x xx     xx+x-  xxxx xxxx ........+
    #% xx xx xx xx  xx xx xx  x+ xx xx  =+=== xx  xx xxxx-xx xx  =x +x xx  xx   xx xx ...+
    #% xx xx xx  -+x+  xxx+   xx xx  xx       xx  xx x+   xx xxx+xx xx  xx xxxx xx  xx ....+~
    for(split/$~/){s,[ $/],,g;/(.)$/;$l=$-[0];/(.)/||next;$_=chr$-+$l;$".=($1=~/$^/)?" \u$_":$_;}print$";
    
      But... \w will match any "word character", which is defined as "alphanumeric plus '_'", so that regex will match '2_4' and '234' as well as '2A4'.

      You'd need either /\d[a-zA-Z]\d+/ or /\d[:alpha:]\d+/.

      Won't \w match any letter, number, or underscore?
      if ($str =~ m/\d[A-Z]\d+/) { print "Ok!\n"; } else { print "Invalid format! Must be: [digit][letter][digit][digit] ... \n +"; }

      Joshua

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Comparing lower to upper case
by insensate (Hermit) on Jun 11, 2002 at 19:41 UTC
    Why not

    $str=~tr/a-z/A-Z/;

    This will leave all the digits alone and just change the lowercase characters to uppercase.
    -Jason
Re: Comparing lower to upper case
by Abigail-II (Bishop) on Jun 12, 2002 at 14:16 UTC
    Figure out if the second character is a letter of the alphabet?
    if (substr ($str, 1, 1) =~ /[[:alpha:]]/) { .. }
    Convert that character to its uppercase form?
    substr ($_, 1, 1) = uc substr ($_, 1, 1);

    Abigail