Re: num to alpha
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Sun, 26 Feb 2006 22:52:46 -0800
The Ghost wrote:
What's the easiest way for me to turn a number into a letter?
2 -> b
5 -> e
26 -> z
27 -> error
my @convert = ( undef, 'a' .. 'z' );
if ( $number > 0 && defined $convert[ $number ] ) {
print "$number = $convert[$number]\n";
}
else {
print "Error: $number is not a valid number.\n";
}
Or:
my %convert = qw[ 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n
15 o 16 p 17 q 18 r 19 s 20 t 21 u 22 v 23 w 24 x 25 y 26 z ];
if ( exists $convert{ $number } ) {
print "$number = $convert{$number}\n";
}
else {
print "Error: $number is not a valid number.\n";
}
Or:
if ( $number >= 1 && $number <= 26 ) {
print "$number = ", chr( $number + ord( 'a' ) - 1 ), "\n";
}
else {
print "Error: $number is not a valid number.\n";
}
John
--
use Perl;
program
fulfillment
.
- Follow-Ups:
- Re: num to alpha
- From: usenet
- Re: num to alpha
- References:
- num to alpha
- From: The Ghost
- num to alpha
- Prev by Date: num to alpha
- Next by Date: problems parsing a DHCP.leases file.
- Previous by thread: num to alpha
- Next by thread: Re: num to alpha
- Index(es):