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

Hello there.

This is, I think, a very basic question, but since I'm a newbie this sort of thing doesn't bother me...

I need a script to ignore the case used by a user in a form field. How can I do that? I only have a wild guess here: I'll need to use tr right?

So, let's assume that I've called that user entry $foo in my script (pretty original huh?)

How do I make $foo case insensitive to the script, so if the user types "Test" or "TeSt" or "test" or "TEST" doesn't make a difference to the script?

Tx in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
Re: Case sensitive
by Fastolfe (Vicar) on Nov 08, 2000 at 21:29 UTC
    I'm assuming you're talking about CGI here, and that you're talking about the value of a form element. In this case, something like lc might be what you need:
    use CGI; my $value = param('field'); # "mY vALuEs RuLE" $value = lc(param('field')); # "my values rule" $value = uc(param('field')); # "MY VALUES RULE"
Re: Case sensitive
by kilinrax (Deacon) on Nov 08, 2000 at 21:32 UTC
    Yeah, tr would work as a way of converting something to lower case:
    $foo =~ tr/A-Z/a-z/;
    And in the spirit of TMTOWTDI:
    $foo = "\Lfoo";
    However, this is probably the most sensible way:
    $foo = lc $foo;
    Lastly, you could use a case-insensitive regex instead.
    For example, if $test is the string you are matching it with:
    if ($foo =~ /^$test$/i) { ... }
Re: Case sensitive
by stephen (Priest) on Nov 08, 2000 at 21:34 UTC
    DaWolf--

    Here're a couple of answers.

    Easiest: you can convert $foo to lowercase, like so:

    if ( lc($foo) eq "test" ) { print "It was test!\n"; }

    Or use a regular expression with 'i' on the end to indicate case-insensitivity:

    if ( $foo =~ /^test$/i) { print "It was test!\n"; }

    stephen

Re: Case sensitive
by DaWolf (Curate) on Nov 09, 2000 at 02:13 UTC
    Thanks, fellas.
    That was exactly what I was looking for.

    Best regards,

    Er Galvão Abbott
    a.k.a. Lobo, DaWolf
    Webdeveloper