Thread: Perl - convert string to number question

1. Perl - convert string to number question

I'm a little confused and wondered if any AO'ers could clarify this issue for me. I've found a few references via google search but either they aren't what I'm looking for or I simply don't understand that they are what i'm looking for. And section 2.3.4 of the llama book isn't very clear on the matter.

my question is simple: In Perl, can I convert a single letter to it's alphabetic numeric equivalent? Meaning, 'a' would equal '1' and so forth.

a sample prog that illustrates this idea would be:

print "Give me a letter: ";
\$letter = &lt;&gt;;
print "Give me a number: ";
\$num = &lt;&gt;;
print \$letter + \$number;

So, if i gave 'a' as the letter and 1 as the number, the prog would print '2' as the answer.

Any thoughts or hints? Or am I reaching and it would be better to build a database table or hash that contained the letter-number mappings?

Cheers,
&lt;0

2. Hey Hey,

I'm not quite sure that a and 1 are equivalents, or that 1 is the numeric equivalent to a. All you want to to know which letter in the alphabet it is. You are going to just have to do if \$letter = a, \$variable = 1 and then use \$variable in your print statement..

Peace,
HT

3. re: letter, number equivalents

To clarify, what I mean is that in the english alphabet we have 26 letters. If we were to number them, a=1, b=2...z=26.

I'm interested in methods to make perl recognize this process in order to convert letters to digits, then perform various operations between numbers, then even convert digits back to letters once the operative manipulations are complete. This is mainly for basic cryptographic hobbying...

I'm not a Perl expert by far, but it doens't seem that Perl will 'natively' recognize the english alphabetic relationship that I'm describing. So, would it be better to build a database or even a Perl hash to hold this data?

If i'm not being clear, i sincerely apologize. Like i stated, I'm just a little confused on how or what would be the best method to construct this letter-number relationship in Perl.

I do appreciate your comments HTRegz. I hope my clarification above helps...

Cheers,
&lt;0

4. Your best bet would just be to ord the characters and use those as mappings:

Code:
```#!/usr/bin/perl

print "Enter a number or letter: ";
chomp(\$input = &lt;&gt;);

# Numbers between 65 and 122 for A-Za-z then check for
# letters between A-Za-z

if (\$input =~ /^\d/) {
if (\$input &gt;= 65 && \$input &lt;= 122) {

} else {
print "\n\rNumeric input '\$input' out of bounds.\n\r";
}
} elsif (\$input =~ /^\w/) {
\$ord = ord(\$input);

if (\$ord &gt;= 65 && \$ord &lt;= 122) {
print "The ord equiv of '\$input' is '\$ord'\n\r";
} else {
print "\n\rInput '\$input' out of bounds.\n\r";
}
} else {
print "I have no idea what you just entered.\n\r";
}```
The above is just a simple prog that shows how to chr() and ord() characters.
If it's a numeric value, ord it and you'll get the alphabetic equivalent (chr). If it's a
letter of some sort, chr it and you'll get the numeric equivalent (ord). This would
be the best way to map letters because then you're relying on their ASCII numeric/text
equivalents.

A = 65, B = 66, C = 67...
a = 97, b = 98, c = 99...
1 = 49, 2 = 50, 3 = 51...

Get it? Hope this helps!

5. re: Vorlin

Yes!

Now it makes sense. I'd actually read something similar about the ASCII relationship from a google search, but I didn't quite understand its application. Now that I see your prog, I can see the Perl logic being leveraged in way that I can understand it and apply to my programming.

Thanks a ton to both Vorlin and HTRegz for taking the time to lend a Perl beginner a hand.

Cheers,
&lt;0

6. "To clarify, what I mean is that in the english alphabet we have 26 letters. If we were to number them, a=1, b=2...z=26. "

while we have 26 letters in english we have 256 charecters in ascii

a small 'a' is (dec.) 97,

http://www.asciitable.com/

7. It's all good in the hood, my brutha from anutha mutha, hehe....

Helping hands, especially in programming....tons more beneficial than feeling the frustration of having no answers and dead-ends everywhere.

lessthanzero, perl is a veritable god when it comes to manipulation of strings/text. Very very good and also great when dealing with cgi. Learn it well and you're on your way to PHP and the like! Best of luck!

Everyone was most helpful in clarifying this for me. I sincerely appreciate it!

Tedob1- Thanks for the link. definitely handy to have around.

Cheers,
&lt;0

9. As always there are several ways to do it.

You could also try this one:

Code:
```sub convert {

\$char = lc(\$_); # make it lowercase

\$scope = "abcdefghijklmnopqrstuvwxyz";

\$index = index(\$scope, \$char);

return (\$index+1) if (\$index &gt;= 0);

print "Didn't find it!\n";

}```

10. What also can be the problem is the fact that the value want assinged the letter but the value 1 because it had a value stored in it this is where perl can get annoying so u need to to put it in quotes in a \$scalar

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

×