Velocity Reviews > Perl > syntax question

# syntax question

Rosco
Guest
Posts: n/a

 08-26-2003
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
Guest
Posts: n/a

 08-26-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). net...
> 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
Guest
Posts: n/a

 08-27-2003
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed). net...
> 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
Guest
Posts: n/a

 08-27-2003
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed). net...
> 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
> };
>