If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Can't get IF statement to work

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-06, 21:25
stevem stevem is offline
Registered User
 
Join Date: Feb 2006
Posts: 7
Can't get IF statement to work

I kindly ask for your help. I can't get my IF statement to work, and I feel really dumb about this ! I'm decoding a URL variable that looks like this:
ODate=Thu%20Feb%2023%2020:16:53%202006


From that string, I pull out $Month with:
$Month = substr($ODate, 4, 3);

When I run tests, $Month CORRECTLY evaluates to "Feb".

But, I cannot get my IF statements, below, to set $mn to "02"... $mn keeps evaluating to "01" no matter what I do. Can anyone tell me what I'm doing wrong? Thank you!

if ($Month == "Jan") {
$mn = "01";
}
if ($Month == "Feb") {
$mn = "02";
}
if ($Month == "Mar") {
$mn = "03";
}
if ($Month == "Apr") {
$mn = "04";
}
if ($Month == "May") {
$mn = "05";
}
if ($Month == "Jun") {
$mn = "06";
}
if ($Month == "Jul") {
$mn = "07";
}
if ($Month == "Aug") {
$mn = "08";
}
if ($Month == "Sep") {
$mn = "09";
}
if ($Month == "Oct") {
$mn = "10";
}
if ($Month == "Nov") {
$mn = "11";
}
if ($Month == "Dec") {
$mn = "12";
}
Reply With Quote
  #2 (permalink)  
Old 02-24-06, 01:12
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
try using the string comparison operator 'eq' instead of the numeric comparison operator '==':

if ($Month eq 'Jan') {

and there is no reason at all to use all those if conditions as you force perl to evaluate them all even though you probably only will have one true value. Use if/elsif/else blocks for that purpose, but there is an even better solution, use a hash:


Code:
%Months = (
   Jan => '01',
   Feb => '02',
   Mar => '03',
   Apr => '04',
   etc
   etc
   etc
   Dec => '12'
);

$Month = $Months{substr($ODate, 4, 3)} || 'default';
print $Month;
Reply With Quote
  #3 (permalink)  
Old 02-24-06, 13:02
stevem stevem is offline
Registered User
 
Join Date: Feb 2006
Posts: 7
Thank you, thank you, thank you!!! I'm a Flash and PHP programmer, so sometimes Perl really gets me confused. Thank you very much for all your help --- it works now!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On