Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Posts
    7

    Unanswered: 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";
    }

  2. #2
    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;

  3. #3
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •