syntax question

Discussion in 'Perl' started by Rosco, Aug 26, 2003.

  1. Rosco

    Rosco Guest

    What is wrong with the syntax in the following bit of code:

    sub convert($dec){
    $hex = $dec;
    if($dec==10){$hex = "A"}
    else{if($dec==11){$hex = "B"}}
    else{if($dec==12){$hex = "C"}}
    else{if($dec==13){$hex = "D"}}
    else{if($dec==14){$hex = "E"}}
    else{if($dec==15){$hex = "F"}}
    return $hex
    };
     
    Rosco, Aug 26, 2003
    #1
    1. Advertising

  2. Why don't you do this:

    sub convert($dec){
    @hex = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
    return @Alpha[$dec];

    and save some headache
    Regards


    > else{if($dec==11){$hex = "B"}}
    > else{if($dec==12){$hex = "C"}}
    > else{if($dec==13){$hex = "D"}}
    > else{if($dec==14){$hex = "E"}}
    > else{if($dec==15){$hex = "F"}}
    > return $hex
    > };





    "Rosco" <> wrote in message
    news:...
    > What is wrong with the syntax in the following bit of code:
    >
    > sub convert($dec){
    > $hex = $dec;
    > if($dec==10){$hex = "A"}
    > else{if($dec==11){$hex = "B"}}
    > else{if($dec==12){$hex = "C"}}
    > else{if($dec==13){$hex = "D"}}
    > else{if($dec==14){$hex = "E"}}
    > else{if($dec==15){$hex = "F"}}
    > return $hex
    > };
    >
     
    GoMonitor/LanOnLine Incorporation, Aug 26, 2003
    #2
    1. Advertising

  3. Rosco

    PapaBear Guest

    And a recode of your snippet would be:

    <CODE>
    sub convert {
    $dec = $_;
    $hex = $dec;
    if($dec==10){$hex = "A"}
    elsif($dec==11){$hex = "B"}
    elsif($dec==12){$hex = "C"}
    elsif($dec==13){$hex = "D"}
    elsif($dec==14){$hex = "E"}
    elsif($dec==15){$hex = "F"}
    return $hex;
    }
    </CODE>

    --
    __________________________________
    Never mind the bear, beware of Papa!


    "Rosco" <> schreef in bericht
    news:...
    > What is wrong with the syntax in the following bit of code:
    >
    > sub convert($dec){
    > $hex = $dec;
    > if($dec==10){$hex = "A"}
    > else{if($dec==11){$hex = "B"}}
    > else{if($dec==12){$hex = "C"}}
    > else{if($dec==13){$hex = "D"}}
    > else{if($dec==14){$hex = "E"}}
    > else{if($dec==15){$hex = "F"}}
    > return $hex
    > };
    >
     
    PapaBear, Aug 27, 2003
    #3
  4. Rosco

    PapaBear Guest

    Or, why not do it the whole number in one time, instead of digit by digit?
    (works for digits as well, of course)

    sub d2h {
    return sprintf "%lx", $_;
    }

    --
    __________________________________
    Never mind the bear, beware of Papa!


    "Rosco" <> schreef in bericht
    news:...
    > What is wrong with the syntax in the following bit of code:
    >
    > sub convert($dec){
    > $hex = $dec;
    > if($dec==10){$hex = "A"}
    > else{if($dec==11){$hex = "B"}}
    > else{if($dec==12){$hex = "C"}}
    > else{if($dec==13){$hex = "D"}}
    > else{if($dec==14){$hex = "E"}}
    > else{if($dec==15){$hex = "F"}}
    > return $hex
    > };
    >
     
    PapaBear, Aug 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    207
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    215
  3. Good Night Moon
    Replies:
    9
    Views:
    284
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    318
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    316
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page