Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

case insensitive IF statement 1

Status
Not open for further replies.
Jun 22, 2007
9
0
0
US
Here's what I'm trying to do:

Code:
my @skipcn = ('KAbel', 'PAbell', 'tabney', 'TYost');
foreach my $compcn (@skipcn)
   {
      if ($compcn eq $cn)
         {
             print $compcn;
         }
   }

This works, but is case sensitive. I'd like to make it non-case sensitive. I found the use of a regex /exp/i is supposed to make case insensitive. I don't know if I'm doing it wrong, but it doesn't work when I do it.

I tried if (/$compcn/i eq $cn) as well as if ($compcn eq /$cn/i) with no success.
 
With regexp:
Code:
if ( $cn =~ /^\Q$compcn\E$/i ) {
   # match, case-insensitive
}

With lc (the way I'd do it):
Code:
if ( lc( $cn ) eq lc( $compcn ) ) {
   # match, case-insensitive
}
 
That worked like a charm. I hadn't found the lc() function. So much to learn, so little time to do it. Thanks for the answer.
 
you could also use uc():

Code:
if ( uc( $cn ) eq uc( $compcn ) ) {
   # match, case-insensitive
}






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top