Keep It Simple, Stupid PerlMonks

### convert letters into a number

by steph_bow (Pilgrim)
 on Aug 30, 2007 at 09:43 UTC Need Help??
steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Dear Monks,

I would like to know if it is possible to convert letters into numbers ?

Let me explain

I would like that A => 1, B => 2, C => 3, etc ...

It is in order to compute a sum for aircraft identifiers

For example, I would like that AD3 (1 + 4 + 3) => 7

Or that FG3 ( 6 + 7 + 3) => 13

Thanks a lot

Replies are listed 'Best First'.
Re: convert letters into a number
by GrandFather (Sage) on Aug 30, 2007 at 09:50 UTC

You can use ord to return the numeric code associated with a (generally ASCII) character.

Perhaps unrelated to what you are actually asking, but there is a problem with your arithmetic. Also, have you considered that simply summing the 'letters' will not give unique codes?

What are you actually trying to achieve with this coding system? I suspect there is a better way to accomplish your objective than this convert and sum technique.

Update: OP reports "I intend to group planes which have similar names for example DAL13 and DAL14, I have already different criteria but I think that making the sum will help to have more discrimination.".

DWIM is Perl's answer to Gödel
Re: convert letters into a number
by buzzthebuzzsaw (Acolyte) on Aug 30, 2007 at 10:53 UTC
You could just make a hash of the letters with the keys the letters and the values the numbers.
Then it would be simply a case of splitting up the string into characters and going through them and if it's a letter it looks it up in the hash.

This is just an example, but it works...:

```\$string = "AFP254";
\$number = 1;
foreach('A'..'Z'){
\$alpha_nums{\$_} = \$number;
\$number++;
}

+
@string = split("", \$string);

\$total = 0;
foreach(@string){
if(/\S/ && !/\d/){
\$_ = \$alpha_nums{\$_};
}
\$total = \$_+\$total;
}
print "\$total\n";
Re: convert letters into a number
by Prof Vince (Friar) on Aug 30, 2007 at 13:46 UTC
Somehow shorter :
```use List::Util qw/sum/;
my %h;
@h{'A' .. 'Z', 0 .. 9} = (1 .. 26, 0 .. 9);
my \$num = sum @h{map uc, split //, \$letters};
Re: convert letters into a number
by andreas1234567 (Vicar) on Aug 30, 2007 at 11:03 UTC
buzzthebuzzsaw was first to the finish line, here's a similar example:
```use strict;
use warnings;

my %hash = ();
\$hash{ chr(\$_) } = \$_ - ord(q{A}) + 1 for (ord(q{A}) .. ord(q{Z}));
while (<DATA>) {
chomp;
my (\$line, \$sum) = (\$_, 0);
my @arr = split(//, \$line);
while (@arr) {
my \$char = shift(@arr);
\$sum += (\$char =~ m/[A-Z]/) ? \$hash{\$char} : \$char;
}
print qq{\$line : \$sum};
}

__DATA__
FG3
ZZ5
BR7
GT3
```\$ perl  -l 636032.pl
FG3 : 16
ZZ5 : 57
BR7 : 27
GT3 : 30
--
Andreas
Re: convert letters into a number
by isync (Hermit) on Aug 30, 2007 at 10:24 UTC
Sounds like a variant of a very simple crypt function. I would look at cpan into the Crypt:: domain and see if there is such a module. If you find one: Might be helpful to look at the source and change the algorithm according to your needs.
Re: convert letters into a number
by GrandFather (Sage) on Aug 30, 2007 at 20:32 UTC

Given the desire expressed elsewhere to "group planes which have similar names" you may find the following helps:

```use warnings;
use strict;

my @aircraft = qw(AD3 DAL13 DAL14 DBR2 DBR4 DBR3 DEHA2 FG3 FG6);
my %byGroup;

push @{\$byGroup{\$_->[1]}}, \$_->[0] for map {[/(([A-Z]+).*)/]} @aircraf
+t;
printf "Group %5s: %s\n", \$_, "@{\$byGroup{\$_}}" for sort keys %byGroup
+;

Prints:

```Group    AD: AD3
Group   DAL: DAL13 DAL14
Group   DBR: DBR2 DBR4 DBR3
Group  DEHA: DEHA2
Group    FG: FG3 FG6

DWIM is Perl's answer to Gödel

Create A New User
Node Status?
node history
Node Type: perlquestion [id://636032]
Approved by GrandFather
help
Chatterbox?
 [marto]: yeah, the whole infrastructure has changed, and video codecs have moved on a great deal [Corion]: marto: Redoing something on new infrastructure sounds fun. At least you can reuse your previous experience :) [marto]: I'm looking forwads to it. The video sources are dying, and they don't have the hardware to play them back (old tape formats, not just VHS + Laserdisc etc) [marto]: I'll be interested to see what difference in terms of file size/quality the new codecs make. I enjoy working on things like this, so that's a bonus [Corion]: Ah, cool! So it's not an inhouse youtube but for a wider consumption

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-07-28 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (425 votes). Check out past polls.