Perl - convert string to number question
Results 1 to 10 of 10

Thread: Perl - convert string to number question

  1. #1
    Senior Member
    Join Date
    Feb 2004
    Posts
    105

    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 = <>;
    print "Give me a number: ";
    $num = <>;
    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?

    Thanks in advance.

    Cheers,
    <0
    Ego is the great Logic killer

  2. #2
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    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
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  3. #3
    Senior Member
    Join Date
    Feb 2004
    Posts
    105

    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,
    <0
    Ego is the great Logic killer

  4. #4
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    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 = <>);
    
    # Numbers between 65 and 122 for A-Za-z then check for
    # letters between A-Za-z
    
    if ($input =~ /^\d/) {
        if ($input >= 65 && $input <= 122) {
    
        } else {
            print "\n\rNumeric input '$input' out of bounds.\n\r";
        }
    } elsif ($input =~ /^\w/) {
        $ord = ord($input);
    
        if ($ord >= 65 && $ord <= 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!
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  5. #5
    Senior Member
    Join Date
    Feb 2004
    Posts
    105

    Lightbulb 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,
    <0
    Ego is the great Logic killer

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,786
    "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/

    sorry Vorlin i just notices your chart at the bottom of your post but he still might find the link helpfull
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  7. #7
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    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!
    We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.

  8. #8
    Senior Member
    Join Date
    Feb 2004
    Posts
    105

    re: helpful comments

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

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


    Cheers,
    <0
    Ego is the great Logic killer

  9. #9
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,403
    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 >= 0);
    
     print "Didn't find it!\n";
    
    }
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  10. #10
    Member
    Join Date
    Apr 2004
    Posts
    32
    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
    Every thing that has a begining has an end.

Posting Permissions

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