num to alpha

num to alpha

am 27.02.2006 07:28:05 von The Ghost

What's the easiest way for me to turn a number into a letter?

2 -> b
5 -> e
26 -> z
27 -> error

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: num to alpha

am 27.02.2006 07:52:46 von krahnj

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

RE: num to alpha

am 28.02.2006 00:18:40 von Timothy Johnson

Just for the sake of one more way to do it:

###########################

use strict;
use warnings;

my %numbers;
my $letter =3D 'a';
my $userinput =3D 23;

foreach(1..26){
$numbers{$_} =3D $letter;
$letter++;
}

if(defined($numbers{$userinput})){
print "It's good!\n";
}else{
print "Invalid number.\n";
}

############################



-----Original Message-----
From: The Ghost [mailto:ghost@madisonip.com]=20
Sent: Sunday, February 26, 2006 10:28 PM
To: Perl Beginners
Subject: num to alpha

What's the easiest way for me to turn a number into a letter?

2 -> b
5 -> e
26 -> z
27 -> error

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org





--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

RE: num to alpha

am 28.02.2006 04:08:44 von gjkeenan

>
>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 ];
>

Hi,

I know John Krahn is a fan of %map. He has advised me on using it before.

Just wondering if there was any reason he chose to use %convert instead in
his example above.

Cheers,
Greg.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: num to alpha

am 28.02.2006 04:50:24 von krahnj

Timothy Johnson wrote:
> Just for the sake of one more way to do it:
>
> ###########################
>
> use strict;
> use warnings;
>
> my %numbers;
> my $letter = 'a';
> my $userinput = 23;
>
> foreach(1..26){
> $numbers{$_} = $letter;
> $letter++;
> }

Or:

my %numbers;

@numbers{ 1 .. 26 } = 'a' .. 'z';


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: num to alpha

am 28.02.2006 05:10:17 von krahnj

Keenan, Greg John (Greg)** CTR ** wrote:
>>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 ];
>>
>
> Hi,

Hello,

> I know John Krahn is a fan of %map.

I am?

> He has advised me on using it before.
>
> Just wondering if there was any reason he chose to use %convert instead in
> his example above.

%map and %convert are just hash names, you can use any name that you want.


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

RE: num to alpha

am 28.02.2006 05:21:35 von gjkeenan

>> I know John Krahn is a fan of %map.

>I am?

>> He has advised me on using it before.
>>
>> Just wondering if there was any reason he chose to use %convert
>> instead in his example above.

>%map and %convert are just hash names, you can use any name that you want.


Sorry - my mistake.

Still trying to get the hang of perl and I've totally confused a few things.
Back to the books for me!

Cheers,
Greg.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: num to alpha

am 28.02.2006 05:55:46 von krahnj

Keenan, Greg John (Greg)** CTR ** wrote:
>>>I know John Krahn is a fan of %map.
>
>>I am?
>
>>>He has advised me on using it before.
>>>
>>>Just wondering if there was any reason he chose to use %convert
>>>instead in his example above.
>
>>%map and %convert are just hash names, you can use any name that you want.
>
>
> Sorry - my mistake.
>
> Still trying to get the hang of perl and I've totally confused a few things.
> Back to the books for me!

Don't worry about it, understanding Perl can be confusing. :-)


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org