# Newbie: test array for lower case

Discussion in 'Perl' started by Mark, Feb 13, 2005.

1. ### MarkGuest

Hi,

I'm having problems with my testing for lower case in an array element.
In the example below, my 'if' statement doesn't seem to test true for lower
case.
It falls through to the block assuming it's upper case.

Anyone know what's wrong?

Thanks.

# Try to determine case and change for consistency
if (\$array[0] eq ["a-z"]) {
print "\narray[0] is lower case: \$array[0]\n";
\$array[0] = lc \$array[0];
\$array[1] = lc \$array[1];
}else{
print "\narray[0] is upper case: \$array[0]\n";
\$array[0] = uc \$array[0];
\$array[1] = uc \$array[1];
}

Mark, Feb 13, 2005

2. ### Jürgen ExnerGuest

Mark wrote:
> Hi,
>
> I'm having problems with my testing for lower case in an array
> element. In the example below, my 'if' statement doesn't seem to test
> true for lower case.
> It falls through to the block assuming it's upper case.
>
> Anyone know what's wrong?
>
> Thanks.
>
> # Try to determine case and change for consistency
> if (\$array[0] eq ["a-z"]) {

What is
["a-z"]
supposed to mean? I may be wrong by to me it looks like you are comparing
the texual value of the first element of @array with an anonymous array,
that has the only element "a-z".

Are you simply trying to check if the string in \$array[0] is all lower case?
Then just do
if (\$array[0] eq lc \$array[0]) {
print "\$array[0] is all lower case\n";
}

jue

Jürgen Exner, Feb 14, 2005

3. ### Joe SmithGuest

Mark wrote:

> # Try to determine case and change for consistency
> if (\$array[0] eq ["a-z"]) {

No, 'eq' does not work that way.

if (\$array[0] eq 'a simple string') { print 'Match' }
if (\$array[0] =~ /[a-z]/) {print 'Has at least one lowercase letter' }
if (\$array[0] =~ /^[a-z]+\$/) {print 'Consists of only lowercase' }

-Joe

Joe Smith, Feb 14, 2005
4. ### MarkGuest

Thanks for both suggestions!

Mark.

"Mark" <> wrote in message
news:67LPd.1824\$...
> Hi,
>
> I'm having problems with my testing for lower case in an array element.
> In the example below, my 'if' statement doesn't seem to test true for
> lower case.
> It falls through to the block assuming it's upper case.
>
> Anyone know what's wrong?
>
> Thanks.
>
> # Try to determine case and change for consistency
> if (\$array[0] eq ["a-z"]) {
> print "\narray[0] is lower case: \$array[0]\n";
> \$array[0] = lc \$array[0];
> \$array[1] = lc \$array[1];
> }else{
> print "\narray[0] is upper case: \$array[0]\n";
> \$array[0] = uc \$array[0];
> \$array[1] = uc \$array[1];
> }
>
>

Mark, Feb 14, 2005