CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Case sensitive 'if' statement



Dec 17, 2000, 4:16 AM

Post #1 of 3 (570 views)
Case sensitive 'if' statement Can't Post

Hi folks,

This is probably a very easy one!

How do I make the following a case sensitive 'if statement':

$var1= "window";
$var2= param(var2);
if($var1 eq $var2){$result="true";}

If var2 is window, I want $result to be 'true'
If var2 is wiNDow, I want $result to be 'true'
If var2 is wind, I want $result to be 'false'
If var2 is door, I want $result to be 'false'

Thanks for your help once again.



Dec 17, 2000, 6:12 AM

Post #2 of 3 (569 views)
Re: Case sensitive 'if' statement [In reply to] Can't Post

It's not so much the if-statement that is case sensitive, as the test you're doing in it. Perl provides the lc() and uc() functions, for returning text in all lowercase or all uppercase. If you test the lowercase version of both strings, that will give you case insensitive equality.

if (lc($this) eq lc($that)) { 
# case insensitive match

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Dec 17, 2000, 6:42 AM

Post #3 of 3 (569 views)
Efficient True/False Values [In reply to] Can't Post

I see a lot of programmers that are new to Perl (and often have some C or C++ background) looking for values in Perl like the bool values true and false. Not finding them, they use strings like "yes" and "no", or "on" and "off", or "true" and "false".

The simplest and easiest way is to use 1 for true, and 0 for false (or, like Perl does it, 1 for true and "" for false). This lets you simply state:

if ($do_something) { print "hi!" } 
else { print "bye!" }

And you can set this "boolean" in one easy step:

$ROOT_USER = ($name eq "root");

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives