|
|
| The Ghost 2006-02-27, 3:58 am |
| What's the easiest way for me to turn a number into a letter?
2 -> b
5 -> e
26 -> z
27 -> error
| |
| John W. Krahn 2006-02-27, 3:58 am |
| 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
| |
| usenet@DavidFilmer.com 2006-02-27, 6:58 pm |
| John W. Krahn 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 ];
Yuck!
Maybe:
$convert{$_} = chr($_ + 96) for (1..26);
--
http://DavidFilmer.com
| |
| DJ Stunks 2006-02-27, 6:58 pm |
| usenet@DavidFilmer.com wrote:
> John W. Krahn wrote:
>
> Yuck!
agreed, but how about a hash slice:
my %convert;
@convert{'a'..'z'} = (1..26);
-jp
PS - no offence John, normally you've got great stuff. :)
| |
| usenet@DavidFilmer.com 2006-02-27, 6:58 pm |
| DJ Stunks wrote:
> @convert{'a'..'z'} = (1..26);
Brilliant!
| |
| usenet@DavidFilmer.com 2006-02-27, 6:58 pm |
| DJ Stunks wrote:
> my %convert;
> @convert{'a'..'z'} = (1..26);
I wonder... Is it possible to combine these two statements into one (as
you can often declare and assign variables in one statement)?
--
http://DavidFilmer.com
| |
| DJ Stunks 2006-02-27, 6:58 pm |
| use...@DavidFilmer.com wrote:
> DJ Stunks wrote:
>
>
> I wonder... Is it possible to combine these two statements into one (as
> you can often declare and assign variables in one statement)?
I have tried, but I wasn't able to under "use strict".
Any ideas, Grandmasters?
-jp
PS - I have never done anything "brilliant", props to _PBP_ for that
one...
| |
| Timothy Johnson 2006-02-27, 6:58 pm |
| 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
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| usenet@DavidFilmer.com 2006-02-27, 6:58 pm |
| DJ Stunks wrote:
> ... props to _PBP_ for that one...
Do you have that page number handy? I must have overlooked that gem
(easy to do; _PBP_ is a gem-rich environment, and my bucket is rather
small). This is somewhat similar to the technique on p.272, but not the
same.
--
http://DavidFilmer.com
| |
| DJ Stunks 2006-02-27, 6:58 pm |
|
usenet@DavidFilmer.com wrote:
> DJ Stunks wrote:
>
> Do you have that page number handy? I must have overlooked that gem
> (easy to do; _PBP_ is a gem-rich environment, and my bucket is rather
> small). This is somewhat similar to the technique on p.272, but not the
> same.
I don't think that exact example was in there, but I, *blush*, didn't
even know about hash slicing before I read _PBP_ and now I find I'm
using it all the time.
I found the stat snippet on p.92 extremely enlightening (not unlike the
rest of the book).
-jp
| |
| Greg John ** CTR ** Keenan 2006-02-27, 9:56 pm |
| >
>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.
| |
| John W. Krahn 2006-02-28, 3:56 am |
| 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
| |
| John W. Krahn 2006-02-28, 3:56 am |
| Keenan, Greg John (Greg)** CTR ** wrote:
> 14 >n
>
> 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
| |
| Greg John ** CTR ** Keenan 2006-02-28, 3:56 am |
| >> I know John Krahn is a fan of %map.
>I am?
[color=darkred]
>%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 a few things.
Back to the books for me!
Cheers,
Greg.
| |
| John W. Krahn 2006-02-28, 3:56 am |
| Keenan, Greg John (Greg)** CTR ** wrote:
>
>
>
>
>
> Sorry - my mistake.
>
> Still trying to get the hang of perl and I've totally a few things.
> Back to the books for me!
Don't worry about it, understanding Perl can be confusing. :-)
John
--
use Perl;
program
fulfillment
| |
| usenet@DavidFilmer.com 2006-02-28, 6:57 pm |
| DJ Stunks wrote:
> use...@DavidFilmer.com wrote:
>
> I have tried, but I wasn't able to under "use strict".
>
> Any ideas, Grandmasters?
I think I'll post this as an independent question (in C.L.P.Misc) - it
is off-topic for this thread anyway. Anyone who is interested may
see/follow/contribute to the thread here:
http://tinyurl.com/nstr5
--
http://DavidFilmer.com
|
|
|
|