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

I am having some trouble with my if/elsif statements and I have even tried just if statements. The below code only returns the outcome of the first statement but never goes to check the next elsif statement. And if I just use plan if statements it only returns the last if statement output. It doesn't matter what is checked it always returns the first.
if($dtype=="SRADDL"){ $ora="SRC Addlf"; }elsif($dtype=="SRSUPP"){ $ora="SRC Supp"; }elsif($dtype=="GROUP"){ $ora="SRC Group"; }elsif($dtype=="SRFXDC"){ $ora="SRC Dep"; }elsif($dtype=="SRFLMD"){ $ora="SRC Med"; }elsif($dtype=="SRLIFE"){ $ora="SRC Life"; }else{ $ora=$dtype; } print $dtype." : ".$ora."\n";
Please help. I have tried everything and it doesn't work.

Replies are listed 'Best First'.
Re: if/elsif statement troubles
by Zaxo (Archbishop) on May 09, 2003 at 21:31 UTC

    You're using numeric equality test, s/==/eq/g.

    Update: ++Enlil, changed to better format on the notional substitution.

    After Compline,
    Zaxo

      thanx.... it was something so simple. It is all fixed now.
Re: if/elsif statement troubles
by VSarkiss (Monsignor) on May 09, 2003 at 21:40 UTC

    Zaxo's answer above is spot on. On a slightly different take, you may wish to use a hash to make your code a little clearer:

    # pick a better name -- I don't know what the data represents my %srhash = ( SRADDL => "SRC Addlf", SRSUPP => "SRC Supp", # and so on ); $ora = $srhash{$dtype} || $dtype;
    HTH

      Slightly better:
      $ora = exists $srhash {$dtype} ? $srhash {$dtype} : $dtype;

      just in case "0", "" or undef is one of the possibilities.

      Abigail

Re: if/elsif statement troubles
by cciulla (Friar) on May 09, 2003 at 21:54 UTC

    How about...

    SWITCH: { $ora = "SRC Addlf", last SWITCH if ($dtype =~ /^SRADDL$/); $ora = "SRC Supp", last SWITCH if ($dtype =~ /^SRSUPP$/); $ora = "SRC Group", last SWITCH if ($dtype =~ /^GROUP$/); $ora = "SRC Dep", last SWITCH if ($dtype =~ /^SRFXDC$/); $ora = "SRC Med", last SWITCH if ($dtype =~ /^SRFLMD$/); $ora = "SRC Life", last SWITCH if ($dtype =~ /^SRLIFE$/); $ora = $dtype; } print "$dtype : $ora \n";

Re: if/elsif statement troubles
by Limbic~Region (Chancellor) on May 09, 2003 at 21:40 UTC
    landonc,
    Since Zaxo has already given you the solution, I can only add that if you do this sort of thing a lot, you might want to consider Switch. It is going to be built into Perl 6 and can save some typing on very large if/elsif/else strings of code.

    Cheers - L~R

      I wouldn't recommend Switch for anything other than experimentation. It's big, and goes through a lot of contortions to work its magic. Either use one of the techniques described in perlsyn or just wait for Perl 6. ;-)

Re: if/elsif statement troubles
by vek (Prior) on May 10, 2003 at 03:16 UTC
    Zaxo has hit the nail on the head here. I also wanted to point out an interesting node of Jeffa's that you might find interesting.

    -- vek --